How to create a variable named from string

I'm loading data from an excel file, with multiple objects. I want to create a variable (matrix) named after the object, containing processed data.
So this is basically what I have:
test_data.name(i) = 'Object 2-1'
This is what i want:
Object_2_1 = [3 4 5 etc.];
This means i both needs to replace "space" and "-" with "_" and then name/create the variable. Can someone please help me on how to approach this challenge?

4 件のコメント

Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
Figured out how to replace space and "-", by using
name = regexprep(test_data.name(i),' ','_');
But still don't know how to "create" the variable named as:
Object_2_1 = [3 4 5 etc.];
Deepak Gupta
Deepak Gupta 2020 年 12 月 3 日
Stephen23
Stephen23 2020 年 12 月 3 日
which states "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
and also this page, which gives plenty of reaons why this approach to writing code should be avoided:
Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
Thank you both very much, both @Deepak for providing a possible answer to my question and to @Stephen for guidance towards another solution which is more correct.

サインインしてコメントする。

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 3 日

0 投票

Although possible, it is a terrible (cannot emphasize enough) coding practice to create variable names dynamically: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval. Avoid it. Either use struct, tables or arrays. It will make your code much efficient and easy to debug.
For example, you can use the test_data struct to contain names as well as data.
test_data(i).name = 'Object 2-1';
test_data(i).data = [3 4 5 etc.];

7 件のコメント

Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
編集済み: Kasper Buchardt 2020 年 12 月 3 日
I see that I had the wrong approach, so I've implemented the solution you suggested by using a struct. Thank you for guiding me towards the right solution. It's easier to learn the correct way from the beginning.
My knowledge of structs was/is somewhat scarse, but now the solution makes sense to me. Thank you!
The solution i've implemented gives a warning though, since it runs in a while-loop. It pops a warning and suggest to preallocate for speed. The program is about 20 min to run to finish (combination of loading files, creating png's and a slow computer). Can you explain to me how to preallocate the struct if i know i = 1:750 and will need 3 variables ('name', 'x_axis', 'y_axis') please.
Ameer Hamza
Ameer Hamza 2020 年 12 月 3 日
編集済み: Ameer Hamza 2020 年 12 月 3 日
Can you show your code? Just pre-allocating the struct array might not be useful. For example, following pre-allocate a struct array of length 100.
s(750) = struct('name', [], 'x_axis', [], 'y_axis', [])
Writing such a thing before your loop will remove the warning message you mentioned.
Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
The part where I store to the struct looks like this:
Processed(i).name = Test_information.Type(i);
Processed(i).x_axis = temp.x_axis;
Processed(i).y_axis = temp.y_axis;
where the struct "temp" is used internally in the while loop and overwritten in each itteration.
I've located the time-consumer to this line of code and guess it's natural (if i disable this line it takes <1 min.):
saveas(fig,(k + " " + temp.name + " " + Connection + " @ " + SupplyVoltage + " V based on " + Test_information.voltage(i) + " V " + Test_information.connection(i) + ".png"))
Output example: 15 IE2 250M-4 Y @ 440 V based on 400 V Y
I don't imagine theres much to optimize in this line, and it's only used for data validtion whenever new data is added (since it's easier to decode a plot than numbers).
Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
BTW it did indeed remove the warning.
Processed(size(Test_data,1)) = struct('Name', [], 'x_axis', [], 'y_axis', []);
Ameer Hamza
Ameer Hamza 2020 年 12 月 3 日
編集済み: Ameer Hamza 2020 年 12 月 3 日
Yes, saveas() need I/O operations, which are naturally slow. Btw, saveas() is quite old and not suitable for high-quality images. See exportgraphics(), which was recently introduced. It might also improve speed. Also see print() or export_fig() from FEX. All these options create much better output then saveas(). You can check the execution speed.
Kasper Buchardt
Kasper Buchardt 2020 年 12 月 3 日
That sounds very interresting and is definitly something i will look into and experiment with.
Thank you yet again.
Ameer Hamza
Ameer Hamza 2020 年 12 月 3 日
I am glad to be of help! :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by