save data to cell array

6 visualizzazioni (ultimi 30 giorni)
Avery Kettlewell
Avery Kettlewell il 1 Apr 2020
Modificato: Ameer Hamza il 1 Apr 2020
I need to save all this data into a cell array in order for my function to work. Can someone please assist me??
while 1
shapeList = {'Circle','Square','Ellipse','Triangle','Rectangle'};
[selection,ok] = listdlg('PromptString','Select the next shape:',...
'SelectionMode','single',...
'OKString','Enter',...
'CancelString','No more',...
'ListString',shapeList);
if ~ok
break;
end
switch selection
case 1
shape='Circle';
case 2
shape='Square';
case 3
shape='Ellipse';
case 4
shape='Triangle';
case 5
shape='Rectangle';
end
shapecounter=shapecounter+1;
switch selection
case 1
prompt={'Enter diameter'};
case 2
prompt={'Enter side length dimension'};
case 3
prompt={'Enter major diameter','Enter minor diameter'};
case 4
prompt={'Enter base dimension', 'Enter height dimension'};
case 5
prompt={'Enter height', 'Enter width'};
end
firstdim=str2num(inputvalues{1});
if length(inputvalues)> 1
seconddim=str2num(inputvalues{2});
end
if isempty(ok)
colour='No_Colour';
else
switch selectionC
case 1
colour='Red';
case 2
colour='Yellow';
case 3
colour='Blue';
case 4
colour='Green';
case 5
colour='Orange';
case 6
colour='Violet';
otherwise
colour='No_Colour';
end
end
switch selection
case 1
area=pi*(firstdim/2)^2;
case 2
area=firstdim^2;
case 3
area=pi*(firstdim/2)*(seconddim/2);
case 4
area=0.5*firstdim*seconddim;
case 5
area=firstdim*seconddim;
end
end
Print_Me(shape);
  2 Commenti
Mohammad Sami
Mohammad Sami il 1 Apr 2020
you either use the {} to store things in cell array
e.g.
a = {shape colour area};
or you can create a cell array and use index to store
a = cell(1,3);
a{1} = shape;
a{2} = colour;
a{3} = area;
Avery Kettlewell
Avery Kettlewell il 1 Apr 2020
Okay thank you!! but now it is saving only the last shape, area, and colour that I put in, it is not saving the ones before that. How do I fix this problem and get it to continually save each shape, area, and colour?

Accedi per commentare.

Risposta accettata

Ameer Hamza
Ameer Hamza il 1 Apr 2020
Modificato: Ameer Hamza il 1 Apr 2020
As suggested by Mohammad Sami, you can use the cell array as described by him. Apart from that, you can significantly simplify your code by removing unnecessary the switch-case blocks. For example, try
counter = 1;
while 1
shapeList = {'Circle','Square','Ellipse','Triangle','Rectangle'};
[selection,ok] = listdlg('PromptString','Select the next shape:',...
'SelectionMode','single',...
'OKString','Enter',...
'CancelString','No more',...
'ListString',shapeList);
if ~ok
break;
end
shape{counter} = shapeList{selection};
prompt_list = {'Enter diameter', 'Enter side length dimension', ...
{'Enter major diameter','Enter minor diameter'}, ...
{'Enter base dimension', 'Enter height dimension'}, ...
{'Enter height', 'Enter width'}};
prompt = prompt_list{selection};
%
% ask for inputvalues
%
inputvalues = {'1'}; % for example
firstdim=str2num(inputvalues{1});
if length(inputvalues{1})> 1
seconddim=str2num(inputvalues{2});
end
if isempty(ok)
colour{counter}='No_Colour';
else
colour_list = {'Red', 'Yellow', 'Blue', 'Green', 'Violet', 'No_Colour'};
colour{counter} = colour_list{selection};
end
switch selection
case 1
area{counter}=pi*(firstdim/2)^2;
case 2
area{counter}=firstdim^2;
case 3
area{counter}=pi*(firstdim/2)*(seconddim/2);
case 4
area{counter}=0.5*firstdim*seconddim;
case 5
area{counter}=firstdim*seconddim;
end
counter = counter + 1;
end
Check the values of shape, colour and area variables.

Più risposte (0)

Categorie

Scopri di più su Surfaces, Volumes, and Polygons in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by