How to store a string data into an array
3 次查看(过去 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.
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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)