Convert char to variable
显示 更早的评论
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.
7 个评论
Carlos Rueda
2021-1-2
Stephen23
2021-1-2
"xIf I should avoid eval and I kind of understand why..."
The problem is not eval in particular, the problem is dynamically accessing variable names:
"...could you please let me know which strategy you recommend?"
Very simple: data should be stored in variables, not in variable names, fieldnames, or the like. Note that meta-data, such as country names, test numbers, etc., is data. In order to use MATLAB efficiently and easily, simply keep all data together as much as possible in arrays and minimize pointless duplication into new arrays. For example, your data are neatly contained in a cell array, where they can be trivially accessed using basic indexing (e.g. in a loop) or many function applied to the entire cell array at once (e.g. string comparisons, regular expressions, etc.). If you split up the data into lots of little separated variables you make accessing and processing your data much harder.
Instead of asking about how to name variables, tell us what you are actually trying to do with this data:
Carlos Rueda
2021-1-2
Image Analyst
2021-1-2
I think you'd just make an array of classes where each class has the name as a property, something like (untested)
allCountryData = country(yourCellArray);
and the constructor would use your cell array to make an array.
fprintf('Country 51 is %s.\n', allCountryData(51).name);
Or something like that. Just a guess - haven't tried it. Or maybe it's
allCountryData = country.construct(yourCellArray);
where you have to explicitly create a method called construct that takes yourCellArray and makes an array of classes out of it.
Stephen23
2021-1-2
"Can a vector or array consist of objects?"
Of course:
Accessing that array will be much much simpler than messing around with dynamic variable names (as Walter Roberson's answer shows, dynamically defining variable names based on input data is very fragile and liable to latent bugs... it is also inefficient and difficult to debug). Keeping your data (of any class) in arrays is by far the best way to use MATLAB.
Walter Roberson
2021-1-2
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?
采纳的回答
更多回答(1 个)
Walter Roberson
2021-1-2
编辑:Walter Roberson
2021-1-2
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)));
end
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 .
类别
在 帮助中心 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
