How can I edit the output of xlsread to make each column of the Excel file a single variable in a structure?
4 views (last 30 days)
Show older comments
I have a set of excel files, each of which contains three columns (2 examples are included). I am converting them to .mat files using the code
files = dir('LIV Data*.xlsx');
for k = 1:numel(files)
[num,text,raw] = xlsread(files(k).name);
[dummy, myName] = fileparts(files(k).name);
save(myName)
end
which works fine. However, I then want each of the columns to be an individual variable. I can load the files using the code
D = 'My Directory';
S = dir(fullfile(D,'LIV Data T= *.mat')); % Makes a structure with all files
N = numel(S); % Counts number of files
for ii = 1:N
T = load(fullfile(D,S(ii).name));
I = T.num
end
which loads each of the data sets as three columns. I then want to separate each of these columns into a single data set so that I can plot e.g. column 1 and column 3 against each other. How can I do this?
0 Comments
Answers (1)
Guillaume
on 7 Nov 2018
Note: Use ~ to ignore a function output instead of creating a dummy variable. So:
[~, myName] = fileparts(files(k).name);
The simplest way to create variables out of your excel data is to forget about the ancient xlsread and use the modern readtable which loads the spreadsheet into a table. If your excel file has a header readtable will detect that and use it to name the variables of the table. Otherwise, it'll use Var1, Var2, ... as names but you can easily change these.
I would recommend you keep the data as a single table rather than then separating it into individual variables.
Note that if you want to plot column 3 against column 1, you don't need to create individual variables for that anyway. With your current code, it's simply:
plot(num(:, 3), num(:, 1));
As a rule, it's never a good idea to split data into individual variables.
0 Comments
See Also
Categories
Find more on Spreadsheets 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!