Convert table format from %s to %f
4 views (last 30 days)
Show older comments
I put together a table with variables that I imported from an excel file and now I am unable to plot them.
I think I need to convert the format of the values from %s to %f, but I am unsure how.
Here is some code that I have:
filename = 'filename'; %Name of file to be imported
[num,txt,raw] = xlsread(filename);
% Universal data grab
[rTime,cTime] = find(strcmp(raw,'Time')); %Top of table (Headers)
NanInd = find(isnan(num(:,1)));
rawNan = [NanInd(1)+rTime-1,1]; % Bottom of table
% Create table
headers = raw(rTime,:);
nHeaders = char(headers);
Headers = strrep(headers,')','_');
Headers = strrep(Headers,'(','_');
Headers = strrep(Headers,'-','_');
Headers = strrep(Headers,'-','_');
Headers = strrep(Headers,' ','_');
Headers = strrep(Headers,'_H','x_T');
T = array2table(raw(rTime+1:rawNan(1),:),'VariableNames',Headers);
T.Time = datetime(datevec(cell2mat(T.Time)),'Format','HH:mm:ss.SSS');
plot(T.Time,T.VarY,'k')
This is the error I get:
"Error using datetime/plot (line 78)
Invalid second data argument"
Can I only plot if my data is format %f and how can I convert to %f?
4 Comments
dpb
on 13 Sep 2019
Don't use the raw data to make the numeric table entries--use the num array instead. xlsread will have already done the numeric conversion. Use the raw data only for the purpose of finding the data locations as have done. (I've done this precise exercise a lot for a bunch of Excel files in use at the college where I'm doing pro bono work...sometimes it's easier to read and find the data range then use that with readtable
You may also find that if you create a spreadsheet import object it may have enough additional flexibility that readtable alone doesn't have...altho you can't find a variable number of headerlines that way always reliably depending on the file structure.
If you can't attach an example file or two, we're pretty much only able to give generic advice rather than explicit soutions, unfortunately.
Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!