Storing and comparing data after performing parfor with basic class function

1 view (last 30 days)
Dear forum,
My goal is to compare diferent energy specta after they have been modified using a class function. I first setup a energy spectrum for my first function f(1). I then generate three addition functions f(2),f(3),f(4). These are then sent into the classdef for analysis. Once the parfor is completed I output the four different spectra in the command window. However, I unable to save these to the workspace for additional analysis. Here is my simple example.
This is my main function:
Test(10,1,10)
function Test(Emax,Emin,nodesE)
originalY=zeros(11,2);
for i=1:nodesE
originalY(i,1)=i*(Emax-Emin)/10+Emin;
originalY(i,2)=ones(length(nodesE),1);
end
fit(1)=masterFitTestObj(nodesE);
fit(1).nodeSpectrumE(:,1)=originalY(1:nodesE,1);
fit(1).nodeSpectrumE(:,2)=originalY(1:nodesE,2);
nodesE=nodesE+1;
for thread=2:4
fit(thread)=masterFitTestObj(nodesE);
fit(thread).nodeSpectrumE(:,1)=originalY(1:nodesE,1);
fit(thread).nodeSpectrumE(:,2)=originalY(1:nodesE,2);
end
parfor thread=1:4
fit(thread).run()
fit(thread).spectrumE
end
end
I first make an x-y (2x10) pair (originalY) then generate three x-y pairs with one more value for each (2x11). I then just change the y-values for each in the classdef code:
classdef masterFitTestObj < handle
properties
Emin=1.0;
spectrumE=zeros(20,2);
nodeSpectrumE=[];
end
methods
function obj=masterFitTestObj(nodesE)
obj.spectrumE=zeros(20,2);
obj.nodeSpectrumE=zeros(nodesE,2);
for i=1:20
obj.spectrumE(i,1)=i*0.5+obj.Emin;
end
end
function run(obj)
spectrumE_int = interp1(obj.nodeSpectrumE(:,1),obj.nodeSpectrumE(:,2),obj.spectrumE(:,1));
spectrumE_int(isnan(spectrumE_int))=0;
obj.spectrumE(:,2) = spectrumE_int+rand(20,1);
end
end
end
The goal is to compare the four different x-y pairs of spectrumE once parfor is done. I am able to have these prinited in the command window with the following:
fit(thread).spectrumE
However, I am unable to save these to the workspace. Is there a way to save the four different spectra to the workspace?
Any help is appreaciated.
~CJ

Answers (1)

Aditya
Aditya on 22 Feb 2024
Hi Chad,
I understand that you are looking to store the results from each “spectrum” after processing them in a parfor loop so that you can work with them in the MATLAB workspace for further analysis.
You can store your results from each “spectrum” into an array or cell array and then pass it to the workspace using different methods:
1. Output Arguments:
You can modify your function to return the results as an output argument. Here's a snippet using a cell array to store the spectra:
function spectraCell = Test(Emax, Emin, nodesE)
% ... (your existing code)
spectraCell = cell(1, 4); % Pre-allocate cell array for the spectra
parfor thread=1:4
fit(thread).run();
spectraCell{thread} = fit(thread).spectrumE; % Store the spectrum in the cell array
end
end
You can then call it from the workspace:
spectraCell = Test(10,1,10);
2. Assignin Function:
If you prefer not to modify the function signature, you can use assignin within your function to pass the spectra directly to the workspace:
function Test(Emax, Emin, nodesE)
% ... (your existing code)
spectraCell = cell(1, 4); % Pre-allocate cell array for the spectra
parfor thread=1:4
fit(thread).run();
spectraCell{thread} = fit(thread).spectrumE; % Store the spectrum in the cell array
end
assignin('base', 'spectraCell', spectraCell);
end
3. Save to File:
Another approach is to save the data into a file using the “save function, and then you can load it whenever necessary:
function Test(Emax, Emin, nodesE)
% ... (your existing code)
spectraCell = cell(1, 4); % Pre-allocate cell array for the spectra
parfor thread=1:4
fit(thread).run();
spectraCell{thread} = fit(thread).spectrumE; % Store the spectrum in the cell array
end
save('spectraCell.mat', 'spectraCell');
end
Then, in the workspace, you can load the data:
load('spectraCell.mat');
For more information on “assignin” function, refer to the MATLAB documentation here:
Hope this resolves your issue!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!