Issue with table data creating strings and recognising as numbers
7 次查看(过去 30 天)
显示 更早的评论
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
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
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
2018-11-21
re date16.date - that was a typo. It should be data16.date. I've corrected the code above.
更多回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!