Accessing variables via .mat files?

6 views (last 30 days)
Zafar Iqbal
Zafar Iqbal on 23 Feb 2016
Edited: Stephen23 on 6 Mar 2016
I have 15 `.m` files which are script files and containing code at least 500 lines each (say `file1.m`, `file2.m`, `file3.m`, `file4.m`, `file5.m`, `file6.m`, `file7.m`, `file8.m`, `file9.m`, `file10.m`, `file11.m`, `file12.m`, `file13.m`, `file14.m`, `file15.m`) and one `Main.m` file which is also a script file.
I want to access all files (all variables of these files) in `Main.m` and plot all variables in `Main.m` file, so that all the variables come to Matlab Workspace.
I already tried `file1` via function method and `.mat` file method but not succeeded yet and `Main.m` gave error
> undefined variable
in `Main.m` file I write `save file1.mat` and `load file1.mat` and also tried it via function but still error.
Can any one tell me via code example how can I do it via `.mat` file method? I observed that `Main.m` when run `load` only one m file variables and not all to workspace.the Main.m code is below
Mainfile.m
%clear all
% close all
% clc
tic
% Main Program which includes all GA M files,all WDO M files and All Hybrid M files ,the hybrid M files are made by combining GA and WDO
% with the Following Changes/Modificatioms made.
% Genetic Algorithm for 1. Deferrable load 2. Non Deferrable Load 3. Interruptible load 4. Non interruptible load 5. Must run Load or Regular load
%here is five m files GA algorithm,five m files of WDO algorithm and five m files of hybrid algorithm total 15 m files want to access all variables of all these files in Main.m file and make plots only in Main.m
% GA Algorithm Files
GAdeferrableload (is .m script file) % 1. Deferrable load appliances Scheduling via GA
save GAdeferrableload.mat
load GAdeferrableload.mat
% disp('Contents of GAdeferrableload.mat:')
% whos('-file','GAdeferrableload.mat')
% whos('-file','GAdeferrableload.mat')
GAnondeferrableload (is .m script file) % 2. Non Deferrable load appliances Scheduling via GA
save GAnondeferrableload.mat
load GAnondeferrableload.mat
% disp('Contents of GAnondeferrableload.mat:')
% whos('-file','GAnondeferrableload.mat')
GAinterruptibleload (is .m script file) % 3. Interruptible load appliances Scheduling via GA
save GAinterruptibleload.mat
load GAinterruptibleload.mat
% disp('Contents of GAinterruptibleload.mat:')
% whos('-file','GAinterruptibleload.mat')
GAnoninterruptibleload (is .m script file) % 4. Non Interruptible load appliances Scheduling via GA
save GAnoninterruptibleload.mat
load GAnoninterruptibleload.mat
% disp('Contents of GAnoninterruptibleload.mat:')
% whos('-file','GAnoninterruptibleload.mat')
%GAmustrunload (is .m script file) % 5. Must run or Regular load load appliances not Scheduled only its unscheduledload is calculated via WDO
% save GAmustrunload.mat
% load GAmustrunload.mat
% disp('Contents of GAmustrunload.mat:')
% whos('-file','GAmustrunload.mat')
%%Wind Driven Optimization Algorithm for 1. Deferrable load 2. Non Deferrable Load 3. Interruptible load 4. Non interruptible load 5. Must run Load or Regular load
WDO_SG_Code_PL_test1mdeferrableload (is .m script file) % 1. Deferrable load appliances Scheduling via WDO
save WDO_SG_Code_PL_test1mdeferrableload.mat
load WDO_SG_Code_PL_test1mdeferrableload.mat
% disp('Contents of WDO_SG_Code_PL_test1mdeferrableload.mat:')
% whos('-file','WDO_SG_Code_PL_test1mdeferrableload.mat')
WDO_SG_Code_PL_test1mnondeferrableload (is .m script file) % 2. Non Deferrable load appliances Scheduling via WDO
save WDO_SG_Code_PL_test1mnondeferrableload.mat
load WDO_SG_Code_PL_test1mnondeferrableload.mat
% disp('Contents of WDO_SG_Code_PL_test1mnondeferrableload.mat:')
% whos('-file','WDO_SG_Code_PL_test1mnondeferrableload.mat')
WDO_SG_Code_PL_test1minterreuptibleload (is .m script file) % 3. Interruptible load appliances Scheduling via WDO
save WDO_SG_Code_PL_test1minterreuptibleload.mat
load WDO_SG_Code_PL_test1minterreuptibleload.mat
% disp('Contents of WDO_SG_Code_PL_test1minterreuptibleload.mat:')
% whos('-file','WDO_SG_Code_PL_test1minterreuptibleload.mat')
WDO_SG_Code_PL_test1mnoninterreuptibleload (is .m script file) % 4. Non Interruptible load appliances Scheduling via WDO
save WDO_SG_Code_PL_test1mnoninterreuptibleload.mat
load WDO_SG_Code_PL_test1mnoninterreuptibleload.mat
% disp('Contents of WDO_SG_Code_PL_test1mnoninterreuptibleload.mat:')
% whos('-file','WDO_SG_Code_PL_test1mnoninterreuptibleload.mat')
% WDO_SG_Code_PL_test1mmustrunload (is .m script file) % 5. Must run or Regular load load appliances not Scheduled only its unscheduledload is calculated via WDO
% save WDO_SG_Code_PL_test1mmustrunload.mat
% load WDO_SG_Code_PL_test1mmustrunload.mat
% disp('Contents of WDO_SG_Code_PL_test1mmustrunload.mat:')
% whos('-file','WDO_SG_Code_PL_test1mmustrunload.mat')
%type ('WDO_SG_Code_PL_test1mmustrunload')
%%Hybrid Algorithm for 1. Deferrable load 2. Non Deferrable Load 3. Interruptible load 4. Non interruptible load 5. Must run Load or Regular load
% GA_WDO_Hybrid_Code_deferrable_load (is .m script file)
% GA_WDO_Hybrid_Code_Non_deferrable_load (is .m script file)
% GA_WDO_Hybrid_Code_Interruptible_load (is .m script file)
% GA_WDO_Hybrid_Code_Non_Interruptible_load (is .m script file)
% GA_WDO_Hybrid_Code_MustRun_load (is .m script file)
%%GA_WDO Hybrid Code Files
GA_WDO_DeferrableLoad (is .m script file)
save GA_WDO_DeferrableLoad.mat
load GA_WDO_DeferrableLoad.mat
% disp('Contents of GA_WDO_DeferrableLoad.mat:')
% whos('-file','GA_WDO_DeferrableLoad.mat')
GA_WDO_NonDeferrableLoad (is .m script file)
save GA_WDO_NonDeferrableLoad.mat
load GA_WDO_NonDeferrableLoad.mat
% disp('Contents of GA_WDO_NonDeferrableLoad.mat:')
% whos('-file','GA_WDO_NonDeferrableLoad.mat')
GA_WDO_InterruptibleLoad (is .m script file)
save GA_WDO_InterruptibleLoad.mat
load GA_WDO_InterruptibleLoad.mat
% disp('Contents of GA_WDO_InterruptibleLoad.mat:')
% whos('-file','GA_WDO_InterruptibleLoad.mat')
GA_WDONonInterruptibleLoad (is .m script file)
save GA_WDONonInterruptibleLoad.mat
load GA_WDONonInterruptibleLoad.mat
% disp('Contents of GA_WDONonInterruptibleLoad.mat:')
% whos('-file','GA_WDONonInterruptibleLoad.mat')
% GA_WDOMustRunLoad (is .m script file)
% save GA_WDOMustRunLoad.mat
% load GA_WDOMustRunLoad.mat
% % disp('Contents of GA_WDOMustRunLoad.mat:')
% % whos('-file','GA_WDOMustRunLoad.mat')
%%Variables
%sum_unschedule_costdlaga=0;
%-----------------------------------------------Plots started here--------------
i tried via function one m file like below.
%[uncostdlaga unloaddlaga unpardlaga schcostdlaga schloaddlaga schpardlaga]=GAdeferrableload(sum_unschedule_costdlaga,sum_unscheduledloaddlaga,PAR_unscheduleddlaga,sum_schedule_costdlaga,sum_scheduledloaddlaga,PAR_schedudlaga);
% Unscheduled and scheduled cost of ga
unscheduledcostga=sum(sum_unschedule_costdlaga+sum_unschedule_costndlaga+sum_un schedule_costilaga+sum_unschedule_costnilaga)/4;
scheduledcostga=sum(sum_schedule_costdlaga+sum_schedule_costndlaga+sum_schedule_costilaga+sum_schedule_costnilaga)/4;
% Unscheduled and scheduled cost of wdo
unscheduledcostwdo=sum(sum_unscheduledcostdlawdo+unschedule_costndlawdo+sum_unscheduleilawdo+sum_unschedulenilawdo)/4;
scheduledcostwdo=sum(sum_E_Cost_SSdlawdo);%+sum_E_Cost_SSndlawdo+sum_E_Cost_SSilawdo+sum_E_Cost_SSnilawdo)/4;
% Unscheduled and scheduled cost of hybrid
unscheduledcosthybrid=sum(sum_unscheduledlahybrid+sum_unschedulendlahybrid+sum_unscheduleilahybrid+sum_unschedulenilahybrid)/4;
scheduledcosthybrid=sum(sum_E_Cost_SSdlahybrid);%+sum_E_Cost_SSndlahybrid+sum_E_Cost_SSilahybrid+sum_E_Cost_SSnilahybrid)/4;
figure
grid on
bar(1,unscheduledcostga,.2,'blue') % unscheduled cost by ga
hold on
bar(2,scheduledcostga,.2,'magenta'); % scheduled cost by ga
hold on
bar(3,unscheduledcostwdo,.2,'green'); % unscheduled cost by wdo
hold on
bar(4,scheduledcostwdo,.2,'cyan'); % scheduled cost by wdo
hold on
bar(5,unscheduledcosthybrid,.2,'black'); % unscheduled cost by hybrid
hold on
bar(6,scheduledcosthybrid,.2,'yellow'); % scheduled cost by hybrid
hold on
set(gca, 'XTick',1:1:6, 'XTickLabel',{'Unscheduled Cost GA','Scheduled Cost GA','Unscheduled Cost WDO','Scheduled Cost WDO','Unscheduled Cost Hybrid','Scheduled Cost Hybrid'})%'Scheduled with PV', 'Generation Cost',
ylabel('Energy Cost($)')
hold on
please observe in the Main.m code i have m files Gadeferrable.m,Ganondeferrable.m and so on which are 15 m files which are script files and i want to use,add etc all variables of these m files in Main.m file and want to make all plots in main.m accessing values of variables from all these m files

Accepted Answer

Explorer
Explorer on 23 Feb 2016
Edited: Explorer on 23 Feb 2016
you can just write name of m-file in command window and m-file will execute.
Let's say the names of files are file1.m, file2.m, ...
Write this in command window:
file1
file2

More Answers (0)

Categories

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by