Reading data out of a character cell array - all values, and save those in a new variable

24 views (last 30 days)
Dear Matlab-Gods out there!
My situation: I created a server to access data from my database. So my data is automatically stored in a cell array. (6 lines with information like time, name, values of my measurement, ...) Now I'd like to visualize these measurements, but first of all of course read out the line of data including those.
Structure: My data is stored in a 6x1400 cell array. I got different data types, from double to char inside of it. For example in line 5 I can find all my measurement values, saved as char.
My approaches so far: If I'd like to access data out of a cell array I try it as followed.
datensatz{5,:};
Typing this into the command window, everything works out fine: I get all the input of row 5. Though if I write it into the editor, all I get for
mw=datensatz{5,:};
messwert=str2num(mw);
is the very first value of my data. Actually I got more than a 1000 values in there. Might this be a problem too?
Can anyone give me a hint on how to proceed? I'd really appreciate any comment!
Best regards.
  2 Comments
Paolo
Paolo on 13 Jun 2018
Edited: Paolo on 13 Jun 2018
str2num can lead to unexpected behaviour because of the eval call, try using str2double
jrbbrt
jrbbrt on 13 Jun 2018
Thanks! So I'm going to use this command instead.
Do you have any ideas why I only get the first value of my data instead of the whole line out of my cell array which I tried to requested via my code?

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 13 Jun 2018
Edited: Stephen23 on 13 Jun 2018
If that row of the cell array contains numeric values stored in char vectors, then you can easily convert these to numeric:
vec = str2double(datensatz(5,:))
  1 Comment
jrbbrt
jrbbrt on 13 Jun 2018
Indeed - super simple! Thank you so much for helping me Stephen!! Somehow I am still mixing up those little details so easily, and I guess that's why I had a hard time on this. Thanks, again :)

Sign in to comment.

More Answers (1)

jrbbrt
jrbbrt on 13 Jun 2018
Edited: jrbbrt on 13 Jun 2018
So I think I found the answer:
[m1 m2 m3]=datensatz{5,1:3};
Using this line of code works for me. Means MATLAB can't typically take the content from these cells I'd like to read out and place them into a single array. So I guess one has to assign to each element separately.
... what is super inconvenient in my situation (with that much columns of data!) :(
  1 Comment
Stephen23
Stephen23 on 13 Jun 2018
Edited: Stephen23 on 13 Jun 2018
"Means MATLAB can't typically take the content from these cells I'd like to read out and place them into a single array"
Taking the contents out of the cell array would give you a whole lot of separate char vectors, basically as separate variables. You could join them into one char array, but this is not likely to be very useful for you. In any case, this is what you are doing now:
[m1 m2 m3]=datensatz{5,1:3};
Why do you want to separate the contents of the cell array into three separate variables? In general, keeping data together makes it much easier to work with. You could simply get the part of the cell array that you are interested in (which is thus just one array):
C = datensatz(5,1:3);
Note the parentheses: you should read about the different ways to index cell arrays:
You indicate that the fifth row contains char vectors, so keeping them in a cell array is quite reasonable, if you only want a subset of datensatz. Or perhaps you could convert the cell array to string.
If that row contains char vectors that represent numeric values, then you can easily convert these to a numeric array: see my answer to know how simple this is.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!