Convert table format from %s to %f

3 views (last 30 days)
Ryan McBurney
Ryan McBurney on 12 Sep 2019
Commented: Ryan McBurney on 13 Sep 2019
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
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.
Ryan McBurney
Ryan McBurney on 13 Sep 2019
You nailed it, dqp. I needed to grab the Time variable from the raw data output and num data for the rest of my variables.
tempT = array2table(raw(rTime+1:rawNan(1),:),'VariableNames',Headers);
tempT.Time = datetime(datevec(cell2mat(tempT.Time)),'Format','HH:mm:ss.SSS');
T = array2table(num(1:NanInd(1)-1,:),'VariableNames',Headers);
T.Time = tempT.Time;
Thanks again Guillaume matlab.lang.makeValidName. Definitely makes my code more applicable to other excel files.

Sign in to comment.

Answers (0)

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!