How do I extract data from multiple matrices with double variable?
11 次查看(过去 30 天)
显示 更早的评论
Hello Matlab Community,
I'm new to Matlab programming so I hope the question is not too trivial but I am struggling to find an answer.
I have a series of Matrices containing measurement results, imported into my workspace from as 'm_NN' (NN being the number of the measurement, in this case NN = 10:15.
Each matrix contains the results from 20 canals (so let's write it as n = 1 : N_chan with N_chan=20).
I want to extract each column, so each measurement for each channel and save it as K_n_NN (so e.g. K_6_14 would be the 6th column of the matrix M_014), and save them as doubles in my workspace. Could someone tell me how to do it? My trials with sprintf(M_0) and num2str(n) result only in errors.
Your help would be greatly appreciated!
0 个评论
采纳的回答
KL
2017-12-14
编辑:KL
2017-12-14
Oh no! Don't do that. When you can store all your data in one variable, why would you prefer a complex way (creating multiple variables). Totally unnecessary. Use indexing to access your data.
So you have 6 measurements (10:15) and 20 channels each. I'm gonna assume each channel has 100 readings.
data = rand(100,20,6); %dummy data to show you how to use indexing
now, you want to access 6th channel from 4th measurement set,
data(:,6,4)
As simple as that.
2 个评论
KL
2017-12-14
This just allows me to display the values of the channel right?
Well, I showed you how to access them. You can do calculations or whatever on it.
variable = data(:,6,4)*something
or
result = functionProcessChannel(data(:,6,4))
I need to regroup them later (connect the measurements on one channel to 1
if you are talking about getting the measurements on channel 1 across all files,
squeeze(data(:,1,:));
so I would like to save them separately
well, if you wanna store them in separate mat file, or csv or xls files, you can do that just by using save command or any other export data methods. But if you are talking about creating a separate variable just for the sake of having them separately, that's not such a great idea. As part of a bigger matrix, this data is already well organized and accessible by using indexing as I have shown you so creating additional variables only makes your code prone to bugs and hard to track.
read this link to know more: https://de.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
how does Matlab know it is supposed to take the files m_NN?
You should import data in such a way that the third dimension of the matrix concerns each file. There are various examples are how to import data from sequence of file. Just a bit of search on this forum will help you. A simple template is,
folderInfo = dir(path);
fileNames = {folderInfo.name}
for k=1:numel(fileNames)
data(:,:,k) = dlmread(fileNames{k});
end
dlmread is just an example again. Hope it helps.
更多回答(1 个)
Image Analyst
2017-12-14
See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
and then don't do what you're trying to do. Leave them as matrices, then when you want/need your single column, say in a for loop or somewhere, you can simply extract it and use it then. No need to store all columns forever in a newly created variables with different names.
for k = 10 : 15
thisFileName = sprintf(......
thisM = readtable(thisFileName); % Or csvread(), importdata(), xlsread(), etc.
thisColumn = thisM(:, columnNumber);
% Do something with thisColumn, like plot it, compare it, or whatever.
end
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!