現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to store a string data into an array
1 回表示 (過去 30 日間)
古いコメントを表示
I have a an array like this:
trainLabels = zeros(315,1);
And I would like to put a string for example 'bass' on every element of that array. I tried using
fold = 'bass';
testLabels(testctr,1) = fold;
but its giving me this error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-4.
回答 (1 件)
madhan ravi
2018 年 12 月 9 日
trainLabels = cell(315,1);
trainLabels(:)={'bass'}
15 件のコメント
Janrex Pensader
2018 年 12 月 9 日
the fold variable is changing every time because it is in a loop and i need to store those into the array.
madhan ravi
2018 年 12 月 9 日
ah then it would be
trainLabels{i} = {your variable} % i is the loop iterator
Janrex Pensader
2018 年 12 月 9 日
It's showing this error now
Unable to perform assignment because brace indexing is not supported for variables of this type.
madhan ravi
2018 年 12 月 9 日
Ah upload your datas as a .mat file and upload the part of the code your trying to store datas
Janrex Pensader
2018 年 12 月 9 日
the data that I need to store are foldernames ang here is my code:
files = dir('C:\Program Files\MATLAB\R2018a\bin\vision\data\');
files(ismember( {files.name}, {'.', '..'})) = [];
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags);
% Print folder names to command window.
trainctr =1;
testctr =1;
trainLabels = zeros(315,1);
testLabels = zeros(336,1);
trainingImages = zeros(32,32,3, 315);%315
testingImages = zeros(32,32,3, 336);%336
%subfolder loop
for k = 1 : length(subFolders)
%FOR DEBUGGING: Print all subfolders
%fprintf('Sub folder #%d = %s\n', k, subFolders(k).nametest(:,:,1,1));
direc = 'C:\Program Files\MATLAB\R2018a\bin\vision\data\';
newDir = strcat(direc,subFolders(k).name);
subname = subFolders(k).name;
%FOR DEBUGGING: Print new Directory
%fprintf('New Dir: %s\n', newDir);
scanDir = dir(newDir);
scanDir(ismember( {scanDir.name}, {'.', '..'})) = [];
scanFlags = [scanDir.isdir];
scanSubFolders = scanDir(scanFlags);
%note category loop
for i = 1 : length(scanSubFolders)
newDir2 = strcat(newDir,'\');
newDir2 = strcat(newDir2,scanSubFolders(i).name);
newDir3 = newDir2;
newDir2 = strcat(newDir2,'\*.png');
imagefiles = dir(newDir2);
nfiles = length(imagefiles);
fprintf('%s',subFolders(k).name);
fprintf('/');
fprintf('%s\n',scanSubFolders(i).name);
fold = scanSubFolders(i).name;
%image loop
for ii=1:nfiles
fprintf('%s\n',imagefiles(ii).name);
%
photo=imagefiles(ii).name;
filename = fullfile(newDir3,photo);
Image = imread(filename);
folder = strcmp('testing',subname);
if folder == 1
test(:,:,:,testctr) = Image;
testLabels{testctr} = {fold};
testctr = testctr+1;
else
train(:,:,:,trainctr) = Image;
trainLabels{trainctr} = {fold};
trainctr = trainctr+1;
end
end
fprintf('\n');
end
fprintf('\n');
end
madhan ravi
2018 年 12 月 9 日
You didn't look into my answer properly! You forgot to add this line before the loop
trainLabels = cell(315,1);
Janrex Pensader
2018 年 12 月 9 日
I added trainLabels = cell(315,1); at the top of the code and the error is gone but when i checked the elements of the array it shows this.
testLabels
testLabels =
336×1 cell array
{1×1 cell}
{1×1 cell}
{1×1 cell}
{1×1 cell}...
madhan ravi
2018 年 12 月 9 日
celldisp(testLabels) %to view the contents of the cell
If my answered solved your question make sure to accept the answer and give a vote.
Janrex Pensader
2018 年 12 月 9 日
are there any other ways to do this? because I'm going to connect this to a code that needs an ordinary array of names.
madhan ravi
2018 年 12 月 9 日
編集済み: madhan ravi
2018 年 12 月 9 日
"I'm going to connect this to a code that needs an ordinary array of names."
why not ? you can easily connect it
Janrex Pensader
2018 年 12 月 9 日
The code I'm going to connect it to is the object detection sample code from mathworks.
My code will replace the loading of cifar data to the cnn. the trainLabels will replace the array trainingLabels, and testLabels will replace testingLabels. if i sue your solution for my probem will this code accept my array?
madhan ravi
2018 年 12 月 9 日
To be frank and honest I don't have any experience with that field but in the link you suggested cell arrays ahd been used , why not try it and find it out?
Janrex Pensader
2018 年 12 月 9 日
It didnt work it cause an error it shows "Invalid training data. Y must be a vector of categorical responses." where y is the trainingLabels
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)