MATLAB Answers


Input(vari​able)=('',​'s'); doesn't work? How would it work?

Asked by Rafael Monteiro on 14 Nov 2012

Hey, I'm learning matlab in the university but my teachers aren't very helpful. I need to make this error go away in order to continue my group work which is due november 25th. The program I'm making should work as a database in which the user can store materials such as metals, ceramics and plastics (with their respective properties associated) and also view the data that was stored stored. So far I've been able to load the database and create a new one if it doesn't exist.

The code I have an error in is as follows:

%%Beginning - Loading/Creating datafile.mat
mati =0;
if exist('datafile.mat', 'file')
mati = mati+1;
%%Let the errors begin:
disp('Do you wish to enter a new material?');
inpmat = input('','s');
if strcmpi(inpmat,'yes')
  disp('Enter the type of the new material');
  tipomat(mati)=input('Metal/Ceramic/Plastic: ','s'); % <-- Error here!

Would really appreciate some insight on this. "Subscripted assignment dimension mismatch."

I can store numbers with

dens(mati)=input('Enter the density of the material [Kg/m3]');

but it seems that saving that in a string doesn't work. My issue here is that I need to enter the type and name of the material while being able to let matlab automatically associate each name to its type and properties, such as density, fusion temperature, strain, stress, etc.


Log in to comment.

1 Answer

Answer by Jan Simon
on 14 Nov 2012
Edited by Jan Simon
on 14 Nov 2012
 Accepted Answer

A string is a vector of type CHAR. Therefore it is not a scalar, if it has more than 1 character. In consequene this fails:

tipomat(mati) = 'your input';

To store strings, a "cell string" is apropriate:

tipomat{mati} = 'your input';   % Curly braces

You can store variables of different type in a "cell" array. If only strings are stored in a cell, it is called "cell string".

  1 Comment

Thanks a lot for this! But how will I now associate this cell string to the rest of the strings? For example, in order to display the data for mati = 1 which would be iron, for example. If I have the following stored in my datafile.mat

tipomat{mati} = Metal
namemat{mati} = Iron
dens(mati) = 5
fpoint(mati) = 1700
maxstress(mati) = 840

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!