Info

This question is closed. Reopen it to edit or answer.

How to write a double or three number array to a .m file and then retrieve it again?

1 view (last 30 days)
I am currently working on a simulator and i need to be able to change an array like the one shown.
=[80.0081 79.6250 79.6392]; %Ampere*hours
I will be changing this number from a GUI executing a .dll(simulator realtime)
the array resides in a .m file so i guess i have to use to/from workspace or something like that but i am strugling to get it right. I have tried to figure it out from the learning pages but something isnt right.
Does anyone have enough knowledge to tell me how i am supposed to do this in a step by step easy way?
The original file i am working with is the lithium battery demo here at matlab.

Answers (1)

James Garritano
James Garritano on 13 Apr 2015
Edited: James Garritano on 13 Apr 2015
You should use save.
To save the array:
fileName = 'CurrentConstants';
dataLocation = 'C:\Projects'
filePath = fullfile(dataLocation,fileName);
currentVals=[80.0081 79.6250 79.6392];
save(filePath,'currentVals')
To use the array
fileName = 'CurrentConstants';
dataLocation = 'C:\Projects'
filePath = fullfile(dataLocation,fileName);
loadedVal=load(filePath);
currentVals = loadedVal.currentVals;
Change "fileName" to the name of the file you'd like to save. Change "dataLocation" to the directory you'd like to save your array.
Explanation:
save let's you save variables in your workspace to a file. If you call save without any arguments, it'll save your entire workspace to a fill named "matlab.mat." If you call it with one argument (a string), it'll save your workspace to a file with that string's name (along with ".mat"). If you call it with two arguments (like in the example above), it'll use the first argument as the output file's name, and put into that file variable specified by the second argument.
fullfile is a way of building a file's path that works regardless of the operating system you're using. You could have hard coded the entire path (e.g., "C:\Projects\CurrentConstants") and that would have worked.
load imports the data into the workspace if you don't have a right hand side equals. (e.g., "load('example.mat')" loads all the data from example into the workspace). This would be dangerous as it could overwrite variables. loadedVal = load('Example') puts the data inside "loadedVal," which is safer.
  1 Comment
Roy Hauge
Roy Hauge on 15 Apr 2015
I think i wasnt clear on my question. The simulator will be made in Simulink, the .m file is currently just a lot of different values that is used for calculations for some of the simscape blocks in the build. This Simulink simulator will then be compiled to a .dll file that we will run with a visio made GUI. I am thinking that i need some kind of block in simulink to link the input from the GUI to the values i need to change.

Community Treasure Hunt

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

Start Hunting!