From: Mark Hard on
Hi guys,

I have code that scans through a textfile for data that I want. What I can get from the code is a cell for e.g.

X =

'nset=_PickedSet2,' 'internal,' 'generate' '1,' '8,' '1'

This is a 1x6 cell. What I want to convert this to is..

nset=_PickedSet2 = [1 8 1];

The final catch is that the numbers can go on for any length, so the cell can be any length so we can ignore positions 2 and 3 but I want the rest of the numbers along the line to be part of a numerical vector (which should be of length i-3)

I don't have much experience working with strings unfortunately so if anyone could help that would be greatly appreciated.

Thanks.

Mark.
From: Husam Aldahiyat on
"Mark Hard" <GerryTheLeper(a)hotmail.com> wrote in message <hkhec4$5el$1(a)fred.mathworks.com>...
> Hi guys,
>
> I have code that scans through a textfile for data that I want. What I can get from the code is a cell for e.g.
>
> X =
>
> 'nset=_PickedSet2,' 'internal,' 'generate' '1,' '8,' '1'
>
> This is a 1x6 cell. What I want to convert this to is..
>
> nset=_PickedSet2 = [1 8 1];
>
> The final catch is that the numbers can go on for any length, so the cell can be any length so we can ignore positions 2 and 3 but I want the rest of the numbers along the line to be part of a numerical vector (which should be of length i-3)
>
> I don't have much experience working with strings unfortunately so if anyone could help that would be greatly appreciated.
>
> Thanks.
>
> Mark.

help eval

DUN DUN DUNNNNNN
From: Walter Roberson on
Mark Hard wrote:


> I have code that scans through a textfile for data that I want. What I
> can get from the code is a cell for e.g.
>
> X =
> 'nset=_PickedSet2,' 'internal,' 'generate' '1,' '8,' '1'
>
> This is a 1x6 cell. What I want to convert this to is..
>
> nset=_PickedSet2 = [1 8 1];

The numeric values can be extracted by str2double([X{4:end}])

But I don't know what it means to have two '=' in a statement,
and it is not valid to have a Matlab identifier that begins with an
underscore. Are you asking for the whole thing to come out as a string?
But if that were the case, why did you ask about extracting numeric
values? Do you need the value assigned to a variable named according to
the first cell, such as nset_PickedSet2 ? If so, then would it be
acceptable to use a structure field?

fname = X{1}(X{1}~='=');
MyStructure.(fname) = str2double([X{4:end}]);
From: Mark Hard on
Walter Roberson <roberson(a)hushmail.com> wrote in message <hkhgcr$hfh$1(a)canopus.cc.umanitoba.ca>...
> Mark Hard wrote:
>
>
> > I have code that scans through a textfile for data that I want. What I
> > can get from the code is a cell for e.g.
> >
> > X =
> > 'nset=_PickedSet2,' 'internal,' 'generate' '1,' '8,' '1'
> >
> > This is a 1x6 cell. What I want to convert this to is..
> >
> > nset=_PickedSet2 = [1 8 1];
>
> The numeric values can be extracted by str2double([X{4:end}])
>
> But I don't know what it means to have two '=' in a statement,
> and it is not valid to have a Matlab identifier that begins with an
> underscore. Are you asking for the whole thing to come out as a string?
> But if that were the case, why did you ask about extracting numeric
> values? Do you need the value assigned to a variable named according to
> the first cell, such as nset_PickedSet2 ? If so, then would it be
> acceptable to use a structure field?
>
> fname = X{1}(X{1}~='=');
> MyStructure.(fname) = str2double([X{4:end}]);

Thank you very much for your helpful reply. The last 2 lines of code you posted is exactly what I want. Unfortunately it gives me an error...

??? Invalid field name: 'nset_PickedSet2,'.

due to the _ or the = or the ,

I don't suppose there's any way of removing these invalid characters from the original string so I can use them? Unfortunately this is how they are read in from the text file.

Thanks again.

Mark.
From: Walter Roberson on
Mark Hard wrote:
> Walter Roberson <roberson(a)hushmail.com> wrote in message

>> fname = X{1}(X{1}~='=');
>> MyStructure.(fname) = str2double([X{4:end}]);

> Thank you very much for your helpful reply. The last 2 lines of code you
> posted is exactly what I want. Unfortunately it gives me an error...

> ??? Invalid field name: 'nset_PickedSet2,'.

Sorry, I overlooked the comma. I also missed an issue with str2double.

fname = x{1}(~ismember(X{1}, '=,'));
MyStructure.(fname_ = str2double(X(4:end));

Notice here that X(4:end) is a cell array: str2double will work on cell
arrays and return a single vector of values. The [X{4:end}] I had
previously would have constructed completely the wrong string and tried
to have it parsed.