MATLAB Answers

Convert char to variable

234 views (last 30 days)
Carlos Rueda
Carlos Rueda on 2 Jan 2021
Edited: per isakson on 2 Jan 2021
Hello all,
I have a huge cell array with countries and their covid19 incidence.
I would like to use those 'chars' to directly create the objects whose names are the country names within my previously defined Class. Is there a ways to convert those chars to used them as variable or object name?
Thanks in advance for any hint.
Walter Roberson
Walter Roberson on 2 Jan 2021
BelongsTo is the region the country object belongs to, i.e., BelongsTo = World if it is a souvereign country like Chile, or Canada if it is a region/state like Ontario (included in Regions)
Consider São Tomé and Príncipe which is a country in Central Africa. Imagine that somehow you managed to get around the problem that MATLAB variable names cannot contain characters such as space or ã or é or í -- imagine that you found a hack that permitted you to do that. Now, São Tomé and Príncipe is a sovereign country so according to what you said, BelongsTo must be World . But if so, then how are you going to link it into the Region object for Central Africa as shown in line 57 of your data file?

Sign in to comment.

Accepted Answer

per isakson
per isakson on 2 Jan 2021
Edited: per isakson on 2 Jan 2021
"by means of OOP, to create an app"
Object Oriented Design isn't easy and there are many different solutions to a problem.
I would use a top-down approach
  • Make a sketch with paper and pencil of the GUI (I assume that the word "app" implies a GUI and that you should use Matlab's App Designer). Imagine how the user shall use the GUI and what results the user shall see. Make notes.
  • Google design pattern model view controler, MVC.
  • Spend ten minutes with youtube on Class Responsibility Collaboration, CRC (or similar)
  • Write a few CRC-cards and try hard to imagine how these classes would solve the problem. Try again. At this stage it is cheap to make changes.
  • Now it's time to start Matlab.
A comment on your proposed class. The properties, BelongsTo and Regions irk.
classdef Land
% Land is a portion of the earth's solid surface distinguishable by boundaries or ownership.
% Words are important and difficult to find. I search a noun that includes both country and
% region
Both countries and regions would be represented by objects of Land. Here the value of Children would be an array of Region-objects (, which could have Childrens like cities).
"Can a vector or array consist of objects?" Yes, object of the same class (and see matlab.mixin.Heterogeneous class)

More Answers (1)

Walter Roberson
Walter Roberson on 2 Jan 2021
Edited: Walter Roberson on 2 Jan 2021
C19T = readtable('covid_data.xlsx');
mask = cellfun(@isempty, C19T.State);
C19T.BelongsTo(mask) = {'World'};
C19T.BelongsTo(~mask) = C19T.Country(~mask);
C19T.Name(mask) = C19T.Country(mask);
C19T.Name(~mask) = C19T.State(~mask);
for R = 1 : height(C19T)
name = C19T.Name(R);
assignin('base', name, country(C19T.Country(R), C19T.BelongsTo(R)));
This fullfills your requirement to create seperate objects whose names are the country names (or state name as appropriate) to hold data.
If you test it , you will find that it crashes when it gets to Nova Scotia or Central Africa, as it is not possible in MATLAB to create a variable name which has a space in it.
To work around this, you will need to use a MATLAB release that is something like 10 or 15 years old, and use a mex file to create a variable with the corrupt name. All current releases check variable names carefully and refuse to create such variables, but old enough versions were sometimes lax on the checking and it was possible. However, you would need to do some testing to find out whether you can find an old version that was lax enough and which also had the modern classdef system of class definitions.
Using a variable with a corrupt variable name such as that is going to be difficult.
You will also have problems if any of the country names contain non-Latin characters such as España or Sudán .




Community Treasure Hunt

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

Start Hunting!