Saving matrices within a cell
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!