Reading and separating data
显示 更早的评论
Hey! Happy Easter folks!
I've been searching two days for an answer to this problem but couldn't figure out yet; there's tons of advise but I have to figure out the best approach for my particular case.
My data set looks like this:
Time 2012/02/13 00:02:00.000; <<- ALL this is in one cell........
place, 1, 2, 9;
item, 11, 14, 18, 21, 22, 24, 27, 31, 32;
value, 724, 1454, 344, 2449, 1683, 197, 463, 2750, 175;
increase, -182, 175, 704, 408, 199, 208, 626, 154, 363;
Time 2012/02/18 00:00:00.000;
place, 1, 2, 5;
item, 11, 18, 22, 27, 32;
value, 2913, 1061, 3365, 2703, 3337;
increase, -624, -862, -820, -596, -505;
(just on here, columns are separated by comma and rows are separated by semicolon)
maximum number of items 32 (i.e., there could be 32 columns of data)
I need to plot Time (X axis) against value (Y axis) for each item.
Problem I am having with "load"/"fopen"/"strmatch"/"xlsread" is I can't specify a column for an item because it varies through the data (for e.g., item 18 above).
For each time the data is collected I need to plot the value of each item. How do I approach getting Matlab to separate this data for me?
Also I was having trouble assigning the single cell containing both characters and numbers as the X value (Time ...).
How should I approach this problem?
appreciate your time!
12 个评论
Stephen23
2015-3-28
It would be easier for us if you simply uploaded your .MAT file, or whatever source file you are referring to, and tell us what you want to do with the data. You can use the paperclip button to upload data files: please do this by either editing your original question or adding a comment.
Jos (10584)
2015-3-28
Is it in a plain ascii text file? If so, can you post a view of the lines?
From you description I gathered that there are 5 rows per set, always starting with the words "Time","place","item","value", and "increase", before the next set of data starts (" ... rows are separated by semicolon) "). Is that correct?
Geant Bepi
2015-3-29
编辑:dpb
2015-3-29
dpb
2015-3-29
"I can get Matlab to read my text files..."
So, show us how you have the data stored; don't make us guess.
Basically, though, it'll be an application of logical addressing to find the location in the (presumably) cell array of the desired item for each case and then retrieve those. Or, alternatively, you convert the variable-sized cell data into a regular array by inserting missing values from which then the indices would be fixed. But, specific code would rely on knowing precisely how you have the data in the first place.
Geant Bepi
2015-3-29
编辑:Image Analyst
2015-3-30
dpb
2015-3-29
I don't have your file (you've resisted the previous requests to attach a file) so can't duplicate whatever it is that importdata does (and I don't have a release later than R2012b, either, so not sure could duplicate what you've got, anyway).
If you won't do the former, at least show us what
whos _your_loaded_variables_
gives.
Geant Bepi
2015-3-30
编辑:Image Analyst
2015-3-30
Image Analyst
2015-3-30
Where did you add the file? You have to BOTH click the "Choose file" button and then the "Attach file" button. You probably forgot to click "Attach file". Also read this http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup so we don't have to correct the formatting for you and you can get it right the first time.
Geant Bepi
2015-3-30
Guillaume
2015-3-30
An Excel file is not a text file. You cannot use textscan and similar to read it. You have to use xlsread or readtable.
Geant Bepi
2015-3-30
编辑:Geant Bepi
2015-3-30
Stephen23
2015-3-30
Excel file formats are either proprietary binary files or compressed XML files, and are certainly not text files. Do not confuse these different formats!
采纳的回答
更多回答(1 个)
Jos (10584)
2015-3-30
My approach would be:
1) read in all the lines of the text file as strings using ';' as a delimiter
C = textread('myfile.txt','%s','delimiter',';') ;
2) parse through all the strings using a loop
k0 = 1 ;
for k=1:5:numel(C) ;
DATA(k0).time = datevec(strread(C{k},'Time %s','delimiter','')) ;
DATA(k0).place = strread(C{k+1}(7:end),'%d','delimiter',',') ; % ignore the first 6 characters
DATA(k0).item = strread(C{k+1}(6:end),'%d','delimiter',',') ;
% etc.
k0 = k0 + 1 ;
end
类别
在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!