Dynamically creating and calling variables

138 visualizzazioni (ultimi 30 giorni)
I am trying to create variable names k1, k2, k3, etc. dynamically, and assign them to a matrix that I create in a function. I struggling with passing the created matrices out of the function localstiff. I tried using varargout to output the matices out of the function, but it didn't work. Also how would I put those then created matrices into a new function, Globalstiff and access those matricies there? I tried accessing these with varargin also with no success. Any help is great!
nom = input('Enter in the number of members >> \n');
nom = [1,nom];
L = [1, 2];
E = [60, 60];
I = [40, 20];
[ k ] = localstiff( L, E, I, nom);
[ S ] = Globalstiff( k{i} , num);
function [ k ] = localstiff( l, e, j, N )
for i = 1:length(N)
n = N(i);
L = l(:,n);
E = e(:,n);
I = j(:,n);
k = [12 6*L -12 6*L; 6*L 4*L^2 -6*L 2*L^2; -12 -6*L 12 -6*L; 6*L 2*L^2 -6*L 4*L^2];
k = k*(E*I/L^3);
eval(sprintf('k%d = [k]', i));
end
end
function [ S ] = Globalstiff( varargin, num )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
b = (num +1)*2;
S = zeros(b);
t = [1;4];
r = [1;4];
for i = 1:length(num)
k = k{i};
p = (i-1)*2;
q = p;
for j = 1:length(t)
for g = 1:length(r)
rk = k(i,j);
i = i + p;
j = j + q;
zo = S(i,j);
zr = rk + zo;
S(i,j) = zr;
i = i - p;
j = j - q;
end
end
end
end

Risposta accettata

Stephen23
Stephen23 il 28 Feb 2015
Modificato: Stephen23 il 12 Set 2023

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by