Saving matrices within a cell

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.

 采纳的回答

Initialize your matrix for all the matrixes
allMatrixes = {};
Then after your if block just append your latest matrix:
allMatrixes = [allMatrixes; {matrix_points}];

4 个评论

Do you mean in the following way?
X % matrix [rows x 2 columns] double
matrix_points = function_main(X);
Within "function_main.m":
count_rows = height(px); % px is an array count_rows x columns
allMatrixes = {};
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
allMatrixes = {allMatrixes; matrix_points};
Yes. Presumably that is in a loop or somehow called several times so that you'll have more than just one cell.
OK, but it didn't work :/
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

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by