Convert table format from %s to %f

조회 수: 7 (최근 30일)
Ryan McBurney
Ryan McBurney 2019년 9월 12일
댓글: Ryan McBurney 2019년 9월 13일
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
dpb
dpb 2019년 9월 13일
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 2019년 9월 13일
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.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by