Saving matrices within a cell

3 views (last 30 days)
Hello!
I want to save all the variable matrices "matrix_points" (which I create inside the function "function_main.m") inside a cell.
I hope I can explain myself better...
I have these lines of code:
X % matrix [rows x 2 columns] double
matrix_points = function_main(X);
Within the function "function_main.m" there is this part of code where I then derive "matrix_points":
count_rows = height(px); % px is an array count_rows x columns
if count_rows == 2
matrix_points = function_1(input);
elseif count_rows == 3
matrix_points = function_2(input);
elseif count_rows == 4
matrix_points = function_3(input);
end
I want to save all the variable matrices "matrix_points" (which I create inside the function "function_main.m") inside a cell.

Accepted Answer

Image Analyst
Image Analyst on 3 Dec 2022
Edited: Image Analyst on 3 Dec 2022
Initialize your matrix for all the matrixes
allMatrixes = {};
Then after your if block just append your latest matrix:
allMatrixes = [allMatrixes; {matrix_points}];
  4 Comments
Alberto Acri
Alberto Acri on 3 Dec 2022
OK, but it didn't work :/
Image Analyst
Image Analyst on 3 Dec 2022
Try this. It should work.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
allMatrixes = {};
for k = 1 : 4
% Create sample px
numPxRows = 1 + randi(3);
px = rand(numPxRows, 2);
count_rows = height(px); % px is an array count_rows x columns
inputArgument = randi(10);
if count_rows == 2
fprintf('count_rows = 2 so calling function_1(%d).\n', inputArgument)
matrix_points = function_1(inputArgument);
elseif count_rows == 3
fprintf('count_rows = 3 so calling function_2(%d).\n', inputArgument)
matrix_points = function_2(inputArgument);
elseif count_rows == 4
fprintf('count_rows = 4 so calling function_3(%d).\n', inputArgument)
matrix_points = function_3(inputArgument);
end
fprintf(' Adding a cell with a %d element vector in it.\n\n', length(matrix_points))
allMatrixes = [allMatrixes; {matrix_points}];
end
% Display it
celldisp(allMatrixes)
%===========================================================================
% Define all functions below.
function output = function_1(n)
output = rand(1, n);
end
function output = function_2(n)
output = 2 * rand(1, n);
end
function output = function_3(n)
output = 3 * rand(1, n);
end

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!