필터 지우기
필터 지우기

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

조회 수: 27 (최근 30일)
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
Paolo
Paolo 2018년 6월 13일
편집: Paolo 2018년 6월 13일
str2num can lead to unexpected behaviour because of the eval call, try using str2double
jrbbrt
jrbbrt 2018년 6월 13일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일
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
jrbbrt
jrbbrt 2018년 6월 13일
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 :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

jrbbrt
jrbbrt 2018년 6월 13일
편집: jrbbrt 2018년 6월 13일
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
Stephen23
Stephen23 2018년 6월 13일
편집: Stephen23 2018년 6월 13일
"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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by