Take two matrices in a folder, take the difference, and save the new matrix to a new folder.

4 views (last 30 days)
Hello everyone, I am new to MatLab and am trying to make a script that will automatically take two specific matrices, take the difference between them as a new matrix, and save that matrix to a new folder.
For example, I have two matrices for one subject named S01_p1 and S01_p2. I would like to form a new matrix from subtracting S01_p1 from S01_p2 and then subsequently save that new matrix in another folder.
Is there any guidance you can provide? I'm presuming it has something to do with a for loop. Sorry if I'm unclear.
  2 Comments
Tushar Behera
Tushar Behera on 8 Feb 2023
Edited: Tushar Behera on 8 Feb 2023
Can you please elaborate from where you want extract these two matrices, is it an excel file or an ASCII file?
Also in the new folder to which format you want to save it to? Provide more details on what you want to do.
Alexander Wang
Alexander Wang on 8 Feb 2023
The two matrices have already been made and are presaved. The matrices are .mat files. I would then want to save it as a .mat file as well to a new folder.
The folder they are in are named "C:\Users\Alex\Desktop\GraphVar\GraphVar_2.03a\workspaces\Winter23_New\data\Matrices GSR" if that helps. If there is any other information you need please don't hesitate to respond.

Sign in to comment.

Answers (2)

Arif Hoq
Arif Hoq on 8 Feb 2023
S01_p1=10:2:20;
S01_p2=1:6;
out=S01_p2-S01_p1; % out is your output
% C:\Users\Documents\New folder is your folder directory
yourdirectory='C:\Users\Documents\New folder\myfile.mat'; % myfile.mat is your matlab file name
save(yourdirectory,'out')

Tushar Behera
Tushar Behera on 8 Feb 2023
Edited: Tushar Behera on 8 Feb 2023
Hi Alexander,
I believe what you want to do here is extract two matrices "S01_p1" and "S01_p2" from their respective ".mat" files and then subtract them and save the result in a new folder.
In order to achieve the above workflow, you can use the "load" function to load the two variables into the workspace and perform your operations upon them. Save them to a separate location using the "save" function. For example:
% Load the two matrices from the .mat files
load('path/to/folder/file1.mat', 'S01_p1');%C:\Users\Alex\Desktop\GraphVar\GraphVar_2.03a\workspaces\Winter23_New\data\Matrices can be replaced with path/to/folder
load('path/to/folder/file2.mat', 'S01_p2');
% Subtract the two matrices
result = S01_p1 - S01_p2;
% Create a new folder if it does not exist
if ~exist('path/to/newfolder', 'dir')
mkdir('path/to/newfolder');
end
% Save the result to a new .mat file in the new folder
save('path/to/newfolder/result.mat', 'result');
I am assuming you have named the two ".mat" files as file1 and file2.
I hope this resolves your question.
Regards,
Tushar

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!