Issue with table data creating strings and recognising as numbers

조회 수: 7 (최근 30일)
Hannah Joyce
Hannah Joyce 2018년 11월 20일
댓글: Hannah Joyce 2018년 12월 2일
I'm working on my minor disseration project - this isn't a necessary step at all, I'm doing it as an attempt to get extra credit. I used MATLAB for my third year disseration last year and imported data in a similar way and did not have this issue. Imported data from excel.
I know why the date isn't working as it isn't a conventional number (a way to convert this would be very helpful also? without editing the excel file if possible) but I don't understand why the others are being processed as strings.
I tried using str2double to fix this but my graph still is empty in terms of numbers so the issue is MATLAB not recognising what I'm giving it as numbers.
I have attached the .mat files.
Any help would be appreciated.
function MAVENdata
a = open('10-9-17 cmes.mat');
b = open('4-8-16 cme.mat');
% import data and convert into an array
c = table2array(a.cmes100917);
d = table2array(b.Untitled);
datec=c(:,1));
nTxc=c(:,2));
nTyc=c(:,3));
nTzc=c(:,4));
nTc=c(:,5));
dated=d(:,1));
nTxd=d(:,2));
nTyd=d(:,3));
nTzd=d(:,4));
nTd=d(:,5));
subplot(2,1,1)
plot(datec, nTxc,'k'); hold on;
plot(datec, nTyc,'r'); hold on;
plot(datec, nTzc,'g'); hold on;
plot(datec, nTc,'b'); hold on;
ylabel('MAG (nT)');
xlabel('Timescale');
grid on
hold off;
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 11월 21일
open() of aa mat file brings up aa variable import dialog if II recall correctly . load() would seem to be more appropriate .

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

채택된 답변

Cris LaPierre
Cris LaPierre 2018년 11월 21일
편집: Cris LaPierre 2018년 11월 21일
What version of MATLAB are you using? If possible, I'd recommend you take advantage of some of the newer features. In particular, I'd recommend datetimes and tables. I modified your mat files back to spreadsheets. Here's how I would implement your code (and it automatically handles the dates).
opts = detectImportOptions('cmes040816.xlsx');
opts = setvartype(opts,'timetagyyyyMMddTHHmmssSSS','datetime');
opts = setvaropts(opts,'timetagyyyyMMddTHHmmssSSS','InputFormat','uuuu-MM-dd''T''HH:mm:ss.SSS');
data16 = readtable('cmes040816.xlsx',opts);
data17 = readtable('cmes100917.xlsx',opts);
data16.Properties.VariableNames = {'date','nTx','nTy','nTz','nT'};
data17.Properties.VariableNames = {'date','nTx','nTy','nTz','nT'};
hold on
plot(data16.date, data16.nTx,'k');
plot(data16.date, data16.nTy,'r');
plot(data16.date, data16.nTz,'g');
plot(data16.date, data16.nT,'b');
hold off;
ylabel('MAG (nT)');
xlabel('Timescale');
grid on
  댓글 수: 6
Cris LaPierre
Cris LaPierre 2018년 11월 21일
re date16.date - that was a typo. It should be data16.date. I've corrected the code above.
Hannah Joyce
Hannah Joyce 2018년 12월 2일
super late response (I've been very busy) but thank you very much!!!

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

추가 답변 (1개)

Brian Hart
Brian Hart 2018년 11월 21일
Hi Hannah,
Try replacing, for example,
nTxc=c(:,2));
with
nTxc=a.cmes100917(:,2).mag_magnetic_field_mso_xnT;
(I used tab completion rather than typing the whole field name).
This gives me a nTxc variable of type double, and with
plot(nTxc,'k')
I get
untitled.jpg

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by