Want to create an array of filenames on every row of a matrix

11 ビュー (過去 30 日間)
Richard Williams
Richard Williams 2017 年 9 月 25 日
コメント済み: Image Analyst 2017 年 9 月 25 日
I am looking to create an array of file names on every row of a matrix,and I keep getting
"In an assignment A(:) = B, the number of elements in A and B must be the same." or "Assignment has more non-singleton rhs dimensions than non-singleton subscripts"
This is what I have right now:
hello = 0;
for i = 1:8
str = strcat('world',' ', num2str(i))
hello(i,:) = str;
end
hello

採用された回答

Geoff Hayes
Geoff Hayes 2017 年 9 月 25 日
Richard - you initialize hello as a scalar instead of as a matrix or cell array so this is going to lead to problems. Try doing the following instead
myFilenames = cell(8,1);
for i = 1:8
str = strcat('world',' ', num2str(i));
myFilenames{i} = str;
end
where
myFilenames =
'world1'
'world2'
'world3'
'world4'
'world5'
'world6'
'world7'
'world8'
  2 件のコメント
Richard Williams
Richard Williams 2017 年 9 月 25 日
Thank you very much for responding so quickly,and explaining where I went wrong! I had one more followup question to your answer:
I am using myFilenames as an input for a csv read via a function that takes in folder/file name and outputs a field name and the filtered csv file as a matrix, to be used to store into structures with various fields defined by the filenames; however, I am getting the same error:
"In an assignment A(:) = B, the number of elements in A and B must be the same."
This is the code that I am using:
file = cell(24,1);
csv = zeros(24,1);
fieldname = zeros(24,1);
for k = 1:24
% create string parts
file{k} = [strcat(v,num2str(k),'_Processed.csv')];
[fieldname(k), csv] = container(folderX, file{k});
end
% create structures
where v is a string, and container is basically
function [fileID, csv] = container (filefolderSV, filename)
data = csvread([filefolderSV filesep filename], 9, 0);
% take out data columns and put into a matrix
%create filename (im not using these names i think, I would prefer not to)
%return
end
Am I making bad initializations again?
Image Analyst
Image Analyst 2017 年 9 月 25 日
Yes. No need as far as I can tell to store all the filenames. Use this:
thisFilename = sprintf('%s%d_Processed.csv', v, k);
[fieldname(k), csv] = container(folderX, thisFilename);
assuming v is a string.
And I can't see that container() is correct - I think a huge portion of it is missing and what's there is almost useless. What you have there would cause an error but not the one you mentioned. It gets data, but doesn't do anything with it, and it returns fileID and csv which it never created. Because so much code is missing, and you did not include the whole error message, just a small part of it, we can't really debug it anymore.

サインインしてコメントする。

その他の回答 (1 件)

Stalin Samuel
Stalin Samuel 2017 年 9 月 25 日
編集済み: Stalin Samuel 2017 年 9 月 25 日
%hello = 0;
for i = 1:8
str = [strcat('world',' ', num2str(i))]
hello{i,:} = str;
end

カテゴリ

Help Center および File Exchangeプログラミング についてさらに検索

Community Treasure Hunt

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

Start Hunting!