Issue with table data creating strings and recognising as numbers
4 views (last 30 days)
Show older comments
Hannah Joyce
on 20 Nov 2018
Commented: Hannah Joyce
on 2 Dec 2018
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 Comment
Walter Roberson
on 21 Nov 2018
open() of aa mat file brings up aa variable import dialog if II recall correctly . load() would seem to be more appropriate .
Accepted Answer
Cris LaPierre
on 21 Nov 2018
Edited: Cris LaPierre
on 21 Nov 2018
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195915/image.png)
6 Comments
Cris LaPierre
on 21 Nov 2018
re date16.date - that was a typo. It should be data16.date. I've corrected the code above.
More Answers (1)
Brian Hart
on 21 Nov 2018
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](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195916/untitled.jpeg)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!