Matrix to numbers in script?

11 views (last 30 days)
Jasmine Larsson
Jasmine Larsson on 4 Oct 2023
Commented: Jasmine Larsson on 9 Oct 2023
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
Stephen23
Stephen23 on 4 Oct 2023
Edited: Stephen23 on 4 Oct 2023
"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
Jasmine Larsson on 9 Oct 2023
Thank you all for the answers, i understand it is not super efficient but it was just for an assignment. I was hoping there was a more efficient or clever ;) way to do this but i will just use copy and paste as @DGM said, which was the original plan. Thanks!!!

Sign in to comment.

Answers (1)

DGM
DGM on 4 Oct 2023
Edited: DGM on 4 Oct 2023
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
ans = '[37 66 56 5 1 5 50 35 62 95 32 2 55 19 99 7 36 28 68 82]'
% ... 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.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!