Matrix to numbers in script?
Show older comments
Hi i dont know if this is possible but, if i have a matrix (1x50 double) named "numbers" that generates from one .m file once all iterations are complete, is it possible to get it written out in a vector called "bestNumbers" in another m.file?
Right now i have:
BestNumbers = [numbers];
But i would like for it to be (in the script not command window):
BestNumbers = [ 1 3 4 5 17 and so on ]
Thankfull for any tips,
/ Jasmine
3 Comments
Dyuman Joshi
on 4 Oct 2023
You can make the 1st script (the one that generates the values) a function file which outputs the variable numbers and call it in the 2nd script.
"is it possible to get it written out in a vector called "bestNumbers" in another m.file?"
That would not be a very easy or efficient approach.
Most likely you should simply pass the data as a function output as Dyuman Joshi recommended, or store it as DGM stated.
Jasmine Larsson
on 9 Oct 2023
Answers (1)
If you're just generating a set of constants to embed into the script, you can use mat2str(), though depending on your actual values and the amount of space you're willing to use, you may have to compromise on precision. For integers, that shouldn't matter.
result_of_calibration = randi(99,1,20);
mat2str(result_of_calibration) % dump to console
% ... then you can copy and paste that into your script like so
calconstants = [37 66 56 5 1 5 50 35 62 95 32 2 55 19 99 7 36 28 68 82];
For non-integer floats, you'll want to select an appropriate output precision.
For larger arrays, especially floats at higher precision, you may be better off just including them in an accompanying .mat file instead of embedding the array in the script as a giant constant.
@Dyuman Joshi also has a good point to consider.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!