Convert table format from %s to %f
조회 수: 7 (최근 30일)
이전 댓글 표시
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
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.
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!