フィルターのクリア

Applying one function in another function

3 ビュー (過去 30 日間)
Bartosz Bagrowski
Bartosz Bagrowski 2022 年 5 月 13 日
回答済み: Voss 2022 年 5 月 13 日
Hi guys, I have two functions:
1) function sol = CreateRandModel(model)
2) function qnew = CreateNeighbor(q,model)
as an output of my first function I get a structure sol where:
sol =
struct with fields:
sum_fburn_rand: 1.2503e+06
fburn_rand: [1×25 double]
arrival_time: [1×25 double]
In the second function I'm only intersted in the field sum_fburn_rand from the output of my first function, I would like to generate in a loop an array of 10 different values based on how this value is generated in the first function. Could you help me with code?

回答 (1 件)

Voss
Voss 2022 年 5 月 13 日
CreateNeighbor()
ans = 1×10
0.3231 0.6672 0.5264 0.2974 0.2440 0.9573 0.2416 0.2394 0.1875 0.8790
function sol = CreateRandomModel()
sol = struct( ...
'sum_fburn_rand',rand(), ...
'fburn_rand',rand(1,25), ...
'arrival_time',rand(1,25));
end
function result = CreateNeighbor()
result = zeros(1,10);
for ii = 1:numel(result)
temp = CreateRandomModel();
result(ii) = temp.sum_fburn_rand;
% or, without the temporary variable 'temp':
result(ii) = getfield(CreateRandomModel(),'sum_fburn_rand');
end
end

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by