MATLAB Answers

Doubles Output in Matrix?

24 views (last 30 days)
nogooduser
nogooduser on 23 Sep 2021 at 21:47
Commented: nogooduser on 29 Sep 2021 at 23:51
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

Accepted Answer

Image Analyst
Image Analyst on 23 Sep 2021 at 21:52
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 Comments
nogooduser
nogooduser on 29 Sep 2021 at 23:51
Hi! Sorry for the delayed response. I plotted and your code works beautifully! Thank you so much for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by