How to plot cell datatype in MATLAB

Hello!
I am here importing data from an Excel file in my workspace. I used readtable command and later convert it into array by using str2array and as a result of this I get cell datatype which has all the correct values. the Excel file contain four variables; Date,Wind speed, Wind angle, Wind power. I also used xlsread function but the output is only 1D array and the remaining arrays were ignored by MATLAB.
data=readtable('sotavento.xlsx');
data=str2array(data);
now, I made a separate 1D array for each variables so that it can be plotted against each other to check the correlation between the given variables.
speed=data(5:end,2); % first 4 points are empty so we discarded.
date=data(5:end,1);
The problem is that I can't plot cell datatype like the way we can plot double etc.
(Note:- the dataset in Excel file may look abnormal as it is quite messy but we are working on it, the main concern is cell datatype plot)

 Accepted Answer

I would simply stay with readtable and go from there.
Here are two options for plotting the data —
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1186153/sotavento.xlsx', 'VariableNamingRule','preserve')
data = 26344×4 table
date Wind speed(m/s) Wind direction Energy (kWh) ____________________ _______________ ______________ ____________ 01-Jan-2020 00:10:00 4.41 12 1.7 01-Jan-2020 00:20:00 4.69 12 5.8 01-Jan-2020 00:30:00 4.6 12 6.9 01-Jan-2020 00:40:00 4.05 12 0 01-Jan-2020 00:50:00 3.87 12 0 01-Jan-2020 01:00:00 4.03 12 0 01-Jan-2020 01:10:00 3.92 12 0 01-Jan-2020 01:20:00 3.8 12 0 01-Jan-2020 01:30:00 3.78 12 0 01-Jan-2020 01:40:00 3.66 12 0 01-Jan-2020 01:50:00 3.35 12 0 01-Jan-2020 02:00:00 3.57 12 0 01-Jan-2020 02:10:00 3.45 12 0 01-Jan-2020 02:20:00 3.38 12 0 01-Jan-2020 02:30:00 3.14 12 0 01-Jan-2020 02:40:00 3.16 12 0
VN = data.Properties.VariableNames;
figure
plot(data.date, data.(VN{2}), 'DisplayName',VN{2})
hold on
plot(data.date, data.(VN{3}), 'DisplayName',VN{3})
plot(data.date, data.(VN{4}), 'DisplayName',VN{4})
hold off
legend('Location','best')
figure
subplot(3,1,1)
plot(data.date, data.(VN{2}))
grid
title(VN{2})
subplot(3,1,2)
plot(data.date, data.(VN{3}))
grid
title(VN{3})
subplot(3,1,3)
plot(data.date, data.(VN{4}))
grid
title(VN{4})
.

4 Comments

Hassan Rizvi
Hassan Rizvi on 9 Nov 2022
Edited: Hassan Rizvi on 9 Nov 2022
Respected strider.
Is VariableNamingRule quite necessery? beacause, I am here using MATLAB 2018a which shows error called:-
Error using readtable (line 197)
Invalid parameter name: VariableNamingRule.
when I use readtable command. What should I do?
Thanks for your elaborative Answer
My pleasure!
Remove that name-value pair argument from the readtable call.
It was previously called 'PreserveVariableNames' and the value was a logical variable true (1) or false (0). I do not remember when it was introduced, however that was apparently after R2018a because that readtable documentation has no mention of it. It is useful because it allows the original variable names to be imported without changing them. Without it, the variable names are converted to acceptable MATLAB variable names.
.
Thanks again sir!

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Community Treasure Hunt

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

Start Hunting!