You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to store a string data into an array
1 view (last 30 days)
Show older comments
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.
Answers (1)
madhan ravi
on 9 Dec 2018
trainLabels = cell(315,1);
trainLabels(:)={'bass'}
15 Comments
Janrex Pensader
on 9 Dec 2018
the fold variable is changing every time because it is in a loop and i need to store those into the array.
madhan ravi
on 9 Dec 2018
ah then it would be
trainLabels{i} = {your variable} % i is the loop iterator
Janrex Pensader
on 9 Dec 2018
It's showing this error now
Unable to perform assignment because brace indexing is not supported for variables of this type.
madhan ravi
on 9 Dec 2018
Ah upload your datas as a .mat file and upload the part of the code your trying to store datas
Janrex Pensader
on 9 Dec 2018
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
on 9 Dec 2018
You didn't look into my answer properly! You forgot to add this line before the loop
trainLabels = cell(315,1);
Janrex Pensader
on 9 Dec 2018
owhh sorry can you explain what that line will do?
Janrex Pensader
on 9 Dec 2018
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
on 9 Dec 2018
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
on 9 Dec 2018
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
on 9 Dec 2018
Edited: madhan ravi
on 9 Dec 2018
"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
on 9 Dec 2018
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
on 9 Dec 2018
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
on 9 Dec 2018
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
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)