How do I extract data from multiple matrices with double variable?

조회 수: 4 (최근 30일)
DiaZulaw
DiaZulaw 2017년 12월 14일
댓글: KL 2017년 12월 14일
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!

채택된 답변

KL
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
DiaZulaw
DiaZulaw 2017년 12월 14일
Thanks a lot. I still don't quite understand.
This just allows me to display the values of the channel right? But I need to regroup them later (connect the measurements on one channel to 1, so I would like to save them separately.
In the rand(100, 20, 6), how does Matlab know it is supposed to take the files m_NN?
It doesnt seem to show the right results when I compare measurement 4, column 6 to what Matlab shows me after this command...
KL
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.
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
Image Analyst 2017년 12월 14일
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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by