Take two matrices in a folder, take the difference, and save the new matrix to a new folder.
1 Ansicht (letzte 30 Tage)
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.
Arif Hoq am 8 Feb. 2023
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
Tushar Behera am 8 Feb. 2023
Bearbeitet: Tushar Behera am 8 Feb. 2023
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
% Subtract the two matrices
result = S01_p1 - S01_p2;
% Create a new folder if it does not exist
if ~exist('path/to/newfolder', 'dir')
% Save the result to a new .mat file in the new folder
I am assuming you have named the two ".mat" files as file1 and file2.
I hope this resolves your question.