Saving matrices within a cell

조회 수: 4 (최근 30일)
Alberto Acri
Alberto Acri 2022년 12월 3일
댓글: Image Analyst 2022년 12월 3일
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.

채택된 답변

Image Analyst
Image Analyst 2022년 12월 3일
편집: Image Analyst 2022년 12월 3일
Initialize your matrix for all the matrixes
allMatrixes = {};
Then after your if block just append your latest matrix:
allMatrixes = [allMatrixes; {matrix_points}];
  댓글 수: 4
Alberto Acri
Alberto Acri 2022년 12월 3일
OK, but it didn't work :/
Image Analyst
Image Analyst 2022년 12월 3일
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개)

카테고리

Help CenterFile 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