Convert nearly created string in a structure format to a variable name in a for loop
Show older comments
Hey there,
I been trying to convert a nearly created string to a variable in a for loop and I cam struggling to find the correct function to convert it.
I have included a simple example problem below which details the problem. Thanks in advance for your help
if true
close all
clear all
clc
% Two cars referened as Car01 and Car02 are studied
% Two variables are recored 1st column= time (s), 2nd column = distanc (m)
A.Car01.speed = [1 2 3 4 5; 10 20 30 40 50 ]';
A.Car02.speed = [1 2.1 3.3 4.5 5; 11 19 33 42 51]';
% Want to interpolate the distance of both cars travelled in a loop
for k=1:2
% Create a mat filename, and load it into a structure called matData.
matFileName = sprintf('A.Car%02d.speed', k);
temp_col = (eval(matFileName));
temp_time = temp_col(:,1);
temp_speed = temp_col(:,2);
Final_lenght = 10;
Interpolation_f = temp_time(end)/Final_lenght;
Interp_time = 0:Interpolation_f:temp_time(end);
temp_speed = interp1(temp_time,temp_speed,Interp_time);
temp_name = sprintf('A.Car%02d.speed_interp', k);
% I want to save the newly created variable A.Car01.speed_interp =
% temp_speed and A.Car02.speed_interp as the other calculated speed in
% the for loop
% cellstr(temp_name) = temp_speed does not work
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!