How can I replace eval and rewrite the below line?

Hi..How can I replace eval..when I tried this way
Speed =Data.(cycle_name(1:end-4)).Data;
Time=Time.(cycle_name(1:end-4)).Time;
it is giving me some error saying Undefined variable "Data" or class "Data.FTP.Data".
eval(['Speed = ' cycle_name(1:end-4) '.Data;']);
eval(['Time = ' cycle_name(1:end-4) '.Time;']);

2 Comments

The solution will be to load your data properly, into an output variable (which happens to be a structure). If you want help with this then please edit your question and upload a sample .mat file so that we can check our code on your data.
Hi ..I am attaching both FTP file and the code

Sign in to comment.

Answers (1)

OCDER
OCDER on 1 Nov 2017
Edited: OCDER on 1 Nov 2017
NEW ANSWER:
Inside the FTP.mat is a variable called ts storing the timeseries object. To access the contents inside ts, do this:
Z = load(fullfile('..\cycledata',cycle_name)) % Z is a temporary variable
Speed = Z.ts.Data;
Time = Z.ts.Time;
OLD ANSWER:
Assuming you have a non-scalar structure called cycle_name that has at least 5 elements, this should work:
Speed = cycle_name(1:end-4).Data;
Time = cycle_name(1:end-4).Time;

16 Comments

Struct contents reference from a non-struct array object.
Error in Vehicle_Model_init (line 41) Speed = cycle_name(1:end-4).Data;..it's not working
load(fullfile('..\cycledata',cycle_name)) eval(['Speed = ' cycle_name(1:end-4) '.Data;']); eval(['Time = ' cycle_name(1:end-4) '.Time;']); Time_interp = (Time(1):Ts:Time(end))' Speed_interp = (interp1(Time,Speed,Time_interp)); This way it works...but i want to replace eval...
Oh, I see - cycle_name is some sort of string/char. What is stored inside this file being loaded? Using load without an output makes it hard to debug, as you're "poofing" variables out of no where into your workspace. Try this:
Z = load(fullfile('..\cycledata',cycle_name))
Speed = Z.(Z.cycle_name(1:end-4)).Data;
Time = Z.(Z.cycle_name(1:end-4)).Time;
Cycle_name='FTP.mat' inside FTP.mat I have Data and Time
Data: [86400×1 double]
DataInfo: [1×1 tsdata.datametadata]
Time: [86400×1 double]
TimeInfo: [1×1 tsdata.timemetadata]
Z = load(fullfile('..\cycledata',cycle_name))
Speed = Z.Data;
Time = Z.Time;
Reference to non-existent field 'Data'.
Error in Vehicle_Model_init (line 41) Speed =Z.Data;
Odd... Try this for debugging purposes on the matlab command window. What are the outputs?
>> load(fullfile('..\cycledata',cycle_name))
>> disp(fullfile('..\cycledata',cycle_name)) %show file name
>> whos %show variables that are loaded
disp(fullfile('..\cycledata',cycle_name)) ..\cycledata\FTP.mat
Name Size Bytes Class Attributes
Ah_zero 1x1 8 double
CurrentFolder 1x55 110 char
FTP 1x1 30362 timeseries
Km_end 1x1 8 double
Q_zero 1x1 8 double
SOC_zero 1x1 8 double
Ts 1x1 8 double
Z 1x1 30538 struct
alpha_J_charge 1x1 8 double
alpha_J_life 1x1 8 double
alpha_J_range 1x1 8 double
alpha_J_speed 1x1 8 double
cycle_name 1x7 14 char
km_percorsi_zero 1x1 8 double
I don't see "Data" or "Time" in this FTP.mat. But you did say FTP.mat has them, right? Where does this come from:
Data: [86400×1 double]
DataInfo: [1×1 tsdata.datametadata]
Time: [86400×1 double]
TimeInfo: [1×1 tsdata.timemetadata]
I think there's some missing info. In the original question, EDIT it to attach the full code (the one you're trying to replace eval with) and FTP.mat file.
Please see the pic..attached one with red circles
@Harini pushparaj: You have been asked twice to upload your .mat file. I will ask a third time: please edit your question and upload a sample .mat file.
Oh.... FTP.mat stores the time series data, ts...
Z = load(fullfile('..\cycledata',cycle_name))
Speed = Z.ts.Data;
Time = Z.ts.Time;
OLD ANS:When i used this Speed = cycle_name(1:end-4).Data; Time = cycle_name(1:end-4).Time; error saying Struct contents reference from a non-struct array object.
Error in Vehicle_Model_init (line 30) Speed = cycle_name(1:end-4).Data; NEW ANS: error saying : Reference to non-existent field 'ts'.
Error in Vehicle_Model_init (line 31) Speed = Z.ts.Data;
Is this the same FTP.mat file, or is there another FTP.mat file? The one you uploaded has a ts timeseries variable.
I have only one FTP file..its the same ...
Starting from the beginning... Set your working directory to the folder storing FTP.mat. Then do this:
clear
load('FTP.mat')
Do you atleast get ts variable show up in your workspace?
Note that "..\cycledata\FTP.mat" means go to the folder above the current directory, then to cycledata folder, then the FTP.mat file inside there. My hunch is, there is another FTP.mat file, just not in your current directory that you want.
The FTP.mat file you showed in the screenshot picture DOES show the ts variable.
HOWEVER, the result from whos command does not show the ts variable, and instead show a variable called FTP that is the timeseries. Hence, it seems you are not loading the correct .mat file.

Sign in to comment.

Categories

Tags

Asked:

on 1 Nov 2017

Commented:

on 1 Nov 2017

Community Treasure Hunt

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

Start Hunting!