como puedo llamar correctamente las variables en un script que proviene de una función

2 visualizzazioni (ultimi 30 giorni)
He desarrollado dos script uno se llama datosiniciales.m (funcion) y el script de simulacion.m
function [variables, values] = datosIniciales(caso, variacion)
% este codigo envia o carga los datos iniciales para un simulink
% tiene dos ciclos for
casos = [1, 2, 3]; % Definimos los casos
variaciones = 1:5; % Cinco variaciones para cada caso
variables = {};
values = {};
% Parametros DFIG -> Parametros de rotor referidos al lado del estator
for caso = casos
for variacion = variaciones
switch caso
case 1 % Variación
B = 2.5e-3;
C = B + D;
switch variacion
case 1, A = B + D;
case 2, A = (B + D) * (1 - 0.05);
case 3, A = (B + D) * (1 - 0.025);
case 4, A = (B + D) * (1 + 0.025);
case 5, A = (B + D) * (1 + 0.05);
end
case 2 % Variación
A = 2.5e-3 + D;
C = 2.5e-3 + D;
switch variacion
case 1, B = 2.5e-3;
case 2, B = 2.5e-3 * (1 - 0.05);
case 3, B = 2.5e-3 * (1 - 0.025);
case 4, B = 2.5e-3 * (1 + 0.025);
case 5, B = 2.5e-3 * (1 + 0.05);
end
case 3 % Variación en Lr (rotor)
B = 2.5e-3;
A = B + D;
switch variacion
case 1, C = B + D;
case 2, C = (B + D) * (1 - 0.05);
case 3, C = (B + D) * (1 - 0.025);
case 4, C = (B + D) * (1 + 0.025);
case 5, C = (B + D) * (1 + 0.05);
end
end
Este scrip envia estos valores al workspace y simulacion.m
% CASO 1, VARIACIÓN 1
%clear all;
[variables, values] = datosIniciales(caso, variacion); % Recalcular variables
% Asignar variables al workspace base
for i = 1:length(variables)
assignin('base', variables{i}, values{i});
end
load_system('DfigV1.slx'); % Recargar el modelo
simOut = sim('DfigV1.slx'); % Ejecutar Simulink
Entonces Al ejecutar simulacion.m ejecuta los valores de caso 3 variacion 5 (que es el ultimo).Pero como estamos en % CASO 1, VARIACIÓN 1 deberia de llamar los datos caso 1 variacion 1 pero ejecuta caso 3 variacion 5.Como realizo una correcta asignacion de variables? Que cuando tenga caso 1 variacion 1 deberia de tener sus valores respectivos y no uno difente. Que me falta adicionar o quitar para ello?
  1 Commento
Walter Roberson
Walter Roberson il 10 Dic 2024
Approximate translation:
How can I correctly call variables in a script that comes from a function?
I have developed two scripts, one is called initialdata.m (function) and the simulation.m script.
So when executing simulation.m it executes the values ​​of case 3 variation 5 (which is the last one). But since we are in % CASE 1, VARIATION 1 it should call the data of case 1 variation 1 but it executes case 3 variation 5. How do I correctly assign variables? That when I have case 1 variation 1 it should have its respective values ​​and not a different one. What do I need to add or remove for this?

Accedi per commentare.

Risposte (1)

Walter Roberson
Walter Roberson il 10 Dic 2024
You have
for caso = casos
for variacion = variaciones
so your code does all of the cases for all of the variations in turn, overwriting A and B and C as it goes. You never save the A B C results.
At the end of the function you return
variables = {};
values = {};
because you never assign to variables or values inside the code.
Well, except for the fact that your code will crash long before it gets there, as D is not defined.

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by