Take two matrices in a folder, take the difference, and save the new matrix to a new folder.
조회 수: 4 (최근 30일)
이전 댓글 표시
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
Tushar Behera
2023년 2월 8일
편집: Tushar Behera
2023년 2월 8일
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.
답변 (2개)
Arif Hoq
2023년 2월 8일
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')
댓글 수: 0
Tushar Behera
2023년 2월 8일
편집: Tushar Behera
2023년 2월 8일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!