Issue with table data creating strings and recognising as numbers

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

open() of aa mat file brings up aa variable import dialog if II recall correctly . load() would seem to be more appropriate .

Sign in to comment.

 Accepted Answer

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 Comments

Also, note that you have an extra closing parentheses when you are extracting data from your array.
datec=c(:,1));
...
using 2018b (just updated this morning), tried running this and I get the error
Error using matlab.io.ImportOptions/getNumericSelection (line
468)
Unknown variable name: 'timetagyyyyMMddTHHmmssSSS'.
Error in matlab.io.ImportOptions/setvartype (line 271)
selection =
opts.getNumericSelection(selection);
Error in MAVENdata (line 4)
opts =
setvartype(opts,'timetagyyyyMMddTHHmmssSSS','datetime');
tried removing the 'timetag' stuff and got
Undefined variable "date16" or class "date16.date".
It is not recognizing the variable name for the first column. What are your column headers? From the data you shared, they appeared to be
  • timetagyyyyMMddTHHmmssSSS
  • mag_magnetic_field_mso_xnT
  • mag_magnetic_field_mso_ynT
  • mag_magnetic_field_mso_znT
  • mag_mso_magnitudenT
That's ok. You don't have to use the actual name. You can use indexing instead. Just modify the setvartype lines of code to
opts = detectImportOptions('cmes040816.xlsx');
opts = setvartype(opts,1,'datetime');
opts = setvaropts(opts,1,'InputFormat','uuuu-MM-dd''T''HH:mm:ss.SSS');
data16 = readtable('cmes040816.xlsx',opts);
data17 = readtable('cmes100917.xlsx',opts);
re date16.date - that was a typo. It should be data16.date. I've corrected the code above.
super late response (I've been very busy) but thank you very much!!!

Sign in to comment.

More Answers (1)

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

Categories

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!