Home > OS >  How to download the workspace into an Excel sheet where I can update a variable in Excel and they up
How to download the workspace into an Excel sheet where I can update a variable in Excel and they up

Time:09-22

Suppose I have a generic MATLAB script as follows:

A = [1 2 0; 2 5 -1; 4 10 -1];
b = [1;3;5];
x = A\b;
r = A*x - b;

These variables will be stored in the Workspace. How can I download the Workspace variables (A, b, x, r) in an Excel sheet, such that I can modify the variables in Excel and upload that Excel sheet unto the Current Folder in MATLAB and have the Workspace updated to the changes I did in Excel? For example, I download the workspace in Excel. I open the Excel sheet and change r=A*x-b to r='Hello World'. Then I upload that sheet onto MATLAB, and the new 'r' updates in the Workspace.

CodePudding user response:

Please consider the following approach as a reference

First, your arrays and operations can be defined as strings, which are then evaluated. Please take this part of my proposal with a grain of salt and make sure that the instructions that you are evaluating are syntactically valid. Keep in mind that the eval function has its own risks

% Clean your workspace
clear
close
clc

% Create your two arrays and additional variables
A = [1 2 0; 2 5 -1; 4 10 -1];
b = [1;3;5];

% Define all the necessary operations as strings. Make sure that these
% operations are absolutely valid before proceeding. Here you can spend
% some time defining some error-checking logic.

x_oper = "A\b";
r_oper = "A*x - b";

% To be safe, we evaluate if the instructions are valid, 
% otherwise we throw an error --> typos and other stuff can go wrong!

try
    x = eval(x_oper);       % be careful! 
    r = eval(r_oper);       % be careful!

    sprintf("Expressions successfully evaluated!")

catch err
    
    sprintf("Error evaluating expression >> %s\n", err.message)
    
end

The values and instructions can be then formatted as individual tables to be saved as .csv files, which can be read using excel (or LibreOffice in my case).

Save your 'workspace' contents into two different files. For the sake of clarity, I am using one file for values and another one for operations

% Define to filenames
varsFile = "pseudo-workspace.csv"
operFile = "operations.csv"

% Convert variables and operations/instructions to tables
dataTable = table(A, b, x, r)
instrTable = table(x_oper, r_oper)

% Write the tables to their respective files
writetable(dataTable, varsFile)
writetable(instrTable, operFile)

Where the dataTable looks like this:

enter image description here

and the instrTable with the operations is:

enter image description here

After this point, your work is saved in two different files and are ready to be edited elsewhere. Perhaps you want to share the file with someone else or yourself in case you don't have access to Matlab on a different computer and you need to change the operations and/or values. Then, on a different .m file you read these files to your current workspace and assign them to the same variable tags:

% Now we read the values and operations from a previous session or
% externally edited in excel/text editor

rawValuesTable = readtable(varsFile)

clear A    % I only clear my variables since I am working on the same m file
clear b
clear x
clear r

% Now we read the values and operations from a previous session or
% externally edited in excel/text editor
rawValuesTable = readtable(varsFile)

% Retrieve the values from A and b from the table that we just read
A = [rawValuesTable.A_1, rawValuesTable.A_2, rawValuesTable.A_3];
b = rawValuesTable.b;

rawOperations = readtable(operFile);

% The operations are read as cell arrays, therefore we need to 
% evaluate them as strings only with the suffix {1}
try 
    
    x = eval(rawOperations.x_oper{1})
    r = eval(rawOperations.r_oper{1})

    sprintf("Expressions successfully evaluated!")
    
catch err
    
    sprintf("Error evaluating expression >> %s\n", err.message)
    
end

Finally obtaining the same output, granted nothing was changed:

enter image description here

You could execute both procedures (write/read) using two different functions. Once again, this is my take on your particular case and you will surely come up with different ideas based on this

  • Related