New to Matlab so please forgive me. I'm trying to create a matrix, mom, that updates with data from the 4th column of each file that's read:
myDir = uigetdir;
my_mom_Files = dir(fullfile(myDir,'MRFbeam-Mom-*.txt'));
mom = [];
for k = 1:length(my_mom_Files)
baseFileName = my_mom_Files(k).name;
fullFileName = fullfile(myDir, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName);
mom_data = load(fullFileName);
mom_col = mom_data(:, 4);
mom = [mom; {mom_col}];
end
The output is as follows:
{ 6181×1 double}
{ 7849×1 double}
{ 3107×1 double}
{ 3107×1 double}
{ 2999×1 double}
{ 2999×1 double}
{ 2000×1 double}
{ 2000×1 double}
{ 2999×1 double}
{ 2999×1 double}
{ 4425×1 double}
{ 6181×1 double}
{ 4425×1 double}
{ 2499×1 double}
{ 2499×1 double}
{11293×1 double}
{11293×1 double}
{29980×1 double}
{29980×1 double}
{ 4825×1 double}
{ 4825×1 double}
{ 1123×1 double}
{ 1123×1 double}
{ 2225×1 double}
{ 2225×1 double}
{ 2221×1 double}
{ 2221×1 double}
{ 7849×1 double}
When I try cell2mat, the size of my matrix changes from 28 x 1 to in the thousands.
Ultimately I would like to print the value found for each file and store it in a matrix. My final goal is to plot these values.
TYIA

 채택된 답변

Image Analyst
Image Analyst 2021년 9월 23일

0 개 추천

Why are you making it a cell array? No need for that. Assuming all the columns are the same height, just stuff it into a column of a preallocated matrix:
myDir = uigetdir;
my_mom_Files = dir(fullfile(myDir,'MRFbeam-Mom-*.txt'));
numFiles = length(my_mom_Files)
numRows = 10; % Whatever -- it's the number of rows in mom_data.
mom4 = zeros(runRows, numFiles);
for k = 1 : numFiles
baseFileName = my_mom_Files(k).name;
fullFileName = fullfile(myDir, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName);
mom_data = load(fullFileName);
thisColumn4 = mom_data(:, 4);
mom4(:, k) = thisColumn4;
end

댓글 수: 6

Thanks! I tried running your code, here's the error it produced:
Unrecognized function or variable 'runRows'.
Error in IMKplot_0P05_test (line 5)
mom4 = zeros(runRows, numFiles);
If runRows was a typo and should be "numRows", and the code is run, here's the other error Matlab produces:
>> IMKplot_0P05_test
Unable to perform assignment because the size of the left side is 6181-by-1 and the size of the
right side is 7849-by-1.
Error in IMKplot_0P05_test (line 12)
mom4(:, k) = thisColumn4;
So you obviously have at least 7848 rows, not 6818. Try this:
myDir = uigetdir;
my_mom_Files = dir(fullfile(myDir,'MRFbeam-Mom-*.txt'));
numFiles = length(my_mom_Files)
numRows = 7849; % Whatever -- it's the number of rows in mom_data.
mom4 = zeros(numRows, numFiles);
for k = 1 : numFiles
baseFileName = my_mom_Files(k).name;
fullFileName = fullfile(myDir, baseFileName);
%fprintf(1, 'Now reading %s\n', fullFileName);
mom_data = load(fullFileName);
thisColumn4 = mom_data(:, 4);
mom4(1 : size(thisColumn4, 1), k) = thisColumn4;
end
nogooduser
nogooduser 2021년 9월 23일
Thanks for answering so fast! My data has different #s of rows which I wasn't expecting. I ran the code and at the end disp(mom4). The output was a matrix of zeros.
Image Analyst
Image Analyst 2021년 9월 24일
Try this:
Then set a breakpoint and learn why thisColumn4 is all zeros. Or why numfiles is zero so the loop never gets entered.
nogooduser
nogooduser 2021년 9월 29일
Hi! Sorry for the delayed response. I plotted and your code works beautifully! Thank you so much for your help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2021년 9월 23일

댓글:

2021년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by