Which function will cause less load to Matlab?

By load, I mean the processing time Matlab needs.
Having 3000 lines of if statements where the statement is constant and only the condition is varied or to use a while(1) with a loop with a evalc and sprintf
while(1) is needed in the loop because it needs to be compared all the time.

 採用された回答

ericson
ericson 2014 年 3 月 14 日

0 投票

Why is it 8 buildings?
ssn = @(sdn) round(24*3600*sdn); iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 ); -I tried running this program in my 2011 version but I can't seem to find what is the function of this code?
I can't also understand this for iwd = 1 :7 for ts = 1 : 12 time_slot_array(ts,1,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts, 0, 0] )); time_slot_array(ts,2,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts,59,59] )); end end -why do you need an to add an initial date to the 2 loops?
for bb = 1 : 8 for ff = 1 : 4 for rr = 1 : 30 Building(bb).Floor(ff).Room(rr).TimeSlot = time_slot_array; end end end -There should be a specific matrix area right? i.e. time_slot_array(2,1,7)

1 件のコメント

per isakson
per isakson 2014 年 3 月 14 日
編集済み: per isakson 2014 年 3 月 14 日
Did you run my code? Answers:
  • "8" - I just picked a number that I thought was reasonable. Didn't we discuss "buildings" in plural? If it is only one building then use Floor(ff).Room(rr).TimeSlot
  • anonymous functions have been around for ten years or more, I guess. round, rem|and |wwekday even longer. Did it throw any error? See http://www.mathworks.se/help/matlab/matlab_prog/anonymous-functions.html
ssn = @(sdn) round(24*3600*sdn);
iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 );
  • I assumed that a time slot has a start and an end point in time. [0,0,iwd-1,7+ts, 0, 0] provides the start and [0,0,iwd-1,7+ts,59,59] the end (59 minutes and 59 seconds later).
  • The code before %% Evaluate red/green ... is just a simple (/cheap) way to provide some dumb sample data.
  • "-why do you need an to add an initial date to the 2 loops?" To indicate that time-slot-data for any week can be generated by changing the value of vec
  • time_slot_array(2,1,7) - I assigned copies of the same data, which covers one week (starting at 2014-03-03 00:00:00), to all rooms (cheap sample data). (Second though: Matlab is smart and took probably advantage of the fact the all rooms had copies of the same data. With different data somewhat more time is probably needed for the comparison.)

サインインしてコメントする。

その他の回答 (1 件)

per isakson
per isakson 2014 年 3 月 10 日
編集済み: per isakson 2014 年 3 月 13 日

0 投票

Both alternative appears a bit problematic
Make an experiment. There is a button [Run and Time].
There must be a third way. Give us a little more background.
.
Step 1. Data structure. What output do you want from your program? That is important to know to choose an appropriate data structure. Maybe, I'm guessing, a "nested" Matlab struct array. "ON" is represented by true and OFF by false. These lines initiate such a struct
clear('day','room')
day = struct( 'slot', false( 1, 12 ) );
room( 1, 120 ) = struct( 'day', repmat( day, [1,7] ) );
Now each one of the "10,080" slots can be accessed in a logical way, e.g.
>> room(45).day(5).slot(9)=true
room =
1x120 struct array with fields:
day
>> room(45).day(5).slot(9)
ans =
1
A logical array might be a better alternative.
What is the input (ON/OFF from the rooms) to the system and what kind of output is required? "Having 3000 lines of if statements [...]" is that an m-file or some kind of output from a building automation system?
.
A timing experiment:
Evaluating the button-color for
  • 8 buildings
  • 4 floors
  • 30 rooms
takes less than 0.04 seconds with my five years old vanilla desktop.
.
All the slot information is stored in a structure array:
Building(8).Floor(4).Room(30).TimeSlot
The field TimeSlot holds all time slot data for one week. (I have not included the check-boxes - yet.)
Initiate one time_slot_array with some simple data
vec = datevec( '2014-03-03 00:00:00' );
time_slot_array = nan( 12, 2, 7 ); % time_slot_array(:,1,:) 1 is begin, 2 end
ssn = @(sdn) round(24*3600*sdn);
iso_week_day = @(sdn) 1 + rem( weekday( sdn - 2 ), 7 );
for iwd = 1 :7
for ts = 1 : 12
time_slot_array(ts,1,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts, 0, 0] ));
time_slot_array(ts,2,iwd) = ssn(datenum( vec + [0,0,iwd-1,7+ts,59,59] ));
end
end
Assign the same time_slot_array to all rooms
Building(8).Floor(4).Room(30).TimeSlot = time_slot_array;
is_red = false( 4, 4, 30 );
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
Building(bb).Floor(ff).Room(rr).TimeSlot = time_slot_array;
end
end
end
Evaluate red/green for all rooms (all will get the color)
cur_sdn = datenum( '2014-03-05 10:52:00' ); % current time
cur_ssn = ssn( cur_sdn );
tic
for bb = 1 : 8
for ff = 1 : 4
for rr = 1 : 30
iwd = iso_week_day( cur_sdn );
is1 = cur_ssn >= Building(bb).Floor(ff).Room(rr).TimeSlot(:,1,iwd);
is2 = cur_ssn <= Building(bb).Floor(ff).Room(rr).TimeSlot(:,2,iwd);
is_red( bb, ff, rr ) = any( is1 & is2 );
end
end
end
toc
outputs
Elapsed time is 0.032368 seconds.

18 件のコメント

ericson
ericson 2014 年 3 月 10 日
I'm creating an energy management system and using Matlab as the GUI. It is used to a classroom setting where the time that the room is ON is based on the input of the user. If the current time and date is the same as the one inputted by the user, then the power in the room is ON.
the code is long because there are 120 rooms. per room, the user can assign 12 time slots on each day. making my conditional statement = 120 x 12 x 7 = 10,080
ericson
ericson 2014 年 3 月 10 日
I'm using 2 guis and I use a mat file to save the data that was inputted by the user in the other gui, which is the time the room is ON.
Here is the main code:
k=weekday(floor(now));
if present==k
if present==7
present=1;
else
present=present+1;
end
for count=1:4
if count==1
var='a';
elseif count==2
var='b';
elseif count==3
var='c';
elseif count==4
var='d';
end
k=weekday(floor(now));
if k==1
word='sun';
elseif k==2
word='mon';
elseif k==3
word='tue';
elseif k==4
word='wed';
elseif k==5
word='th';
elseif k==6
word='fr';
elseif k==7
word='sat';
end
for loop=1:30
for f=1:12
  • %all the codes before this is used to generate the loops that will be used to load the mat file and the variables *
evalstr=sprintf('temp = load(''%s%d_compare.mat'')',var,loop);
evalc(evalstr);
data=temp.data;
tag = fieldnames(data);
evalstr=sprintf('%s%d%s%d=data.%s%d%s%d.string',var,loop,word,f,var,loop,word,f);
evalc(evalstr);
evalstr=sprintf('%s%d%s%d=str2num(%s%d%s%d)',var,loop,word,f,var,loop,word,f);
evalc(evalstr);
%the one with the f is used for the comparison of time. Because the current time should be greater than var but less than fvar
evalstr=sprintf('f%s%d%s%d=data.f%s%d%s%d.string',var,loop,word,f,var,loop,word,f);
evalc(evalstr);
evalstr=sprintf('f%s%d%s%d=str2num(f%s%d%s%d)',var,loop,word,f,var,loop,word,f);
evalc(evalstr);
end
end
end
end
after all the variables are generated, it will then be compared to the time. If the current time is in between the time specified of the user having the same date (mon, tues, etc.), then the background color of the push button will change
Walter Roberson
Walter Roberson 2014 年 3 月 10 日
You should be using indexing. For example,
weekdays = {'sun', 'mon', 'tue', 'wed', 'th', 'fr', 'sat'};
word = weekdays{k};
per isakson
per isakson 2014 年 3 月 10 日
編集済み: per isakson 2014 年 3 月 10 日
These are the strings, which are evaluated with evalc in the first execution of the inner loop.
temp = load('a1_compare.mat')
a1mon1 = data.a1mon1.string
a1mon1 = str2num(a1mon1)
fa1mon1 = data.fa1mon1.string
fa1mon1 = str2num(fa1mon1)
Most of the characters in the variable names are in effect indices. How do you plan to use these variables? There will be many!
Questions
  • count and loop are they "group of buildings" and "building in group"?
  • f is that "slot"?
a1mon1 stands for
  • building group "a"
  • building "1" (in group "a")
  • day of week "mon"
  • slot "1"
Am I right in my guessing? So far the names of the variables. The values are converted from string to numerical.
This code is neither efficient nor programmer friendly. However, more information is needed to propose something better.
More questions
  • Are there 120 mat-files, one for each building?
  • What is the structure of the content of the mat-files?
  • Names such as a1mon1 are used in the mat-files?
  • Every mat-files uses different names for the variables?
ericson
ericson 2014 年 3 月 11 日
count and loop are used to generate the strings a1-a30, b1-b30, c1-c30, and d1-d30
the f is used for the comparison between the var and fvar because the current time should be in between the two given time.
let say var=12:00 and fvar=13:00, the current time should be in between of it so that the background color of the pushbutton will turn to red.
a1mon1 stands for
a=1st floor
1(after a)=room 1
mon=date to be used
1(after mon)=slot number
for the other questions
-in one building there are 240 mat files, 2 mat files per room(1 for the schedule and 1 for the comparing of the schedule)
-a mat file for the schedule was created because if I put the command in the main program, the gui hangs for a while because of the processing of the code.
-there are handles stored in the mat file, its main purpose is to store the data inputted by the user in the other gui
-yes, a1mon1 are used in the mat files, they are stored in the compare mat file
-yes, their main difference is at the start of the name which is a1, b1 , c30 etc.
here is screenshot of the gui
https://www.dropbox.com/s/bft2ajuf2gh2sm2/sample%201.jpg
https://www.dropbox.com/s/w1ecibue8il35v5/sample.jpg
the other mat file is used only when the push button in the main gui is pushed, the specific mat file will be loaded and the data in it will be shown in the right side of the gui
per isakson
per isakson 2014 年 3 月 11 日
編集済み: per isakson 2014 年 3 月 11 日
  • In your problem domain you have buildings, floors, rooms, Used/Vacant (ON/OFF), Week, Day Hour. It would make programming and maintenance much simpler if those words are reflected in the names of the variables.
  • You use a complicated way of "indexing" the variables. Matlab offer good support for indexing, e.g. I find floor(1) better than "a" in position one.
  • Your original question was about efficiency. I think that the major issue regarding efficiency is the communication between the GUIs is via many small mat-files. Wouldn't callbacks be more efficient? However, is efficiency a problem?
  • Your program will spend most of the time checking mat-files, which have not been changed since the last check. Is that so?
ericson
ericson 2014 年 3 月 11 日
  • Ok, I'll change my variable names
  • How do you suggest I do my indexing? like floor(x).room(y).date(i).slot(z)?
  • I'm currently using callbacks in my gui, but the background of the pushbuttons should change real-time depending on the current time and date, making callbacks impossible. I think efficiency is my problem when it comes to the comparing of the time because the current time is continuously being compared to 120 rooms with 12 slots each, depending on the date.
  • As long as the user will not change the range of time per slot, then yes, the mat file will not be changed
per isakson
per isakson 2014 年 3 月 11 日
編集済み: per isakson 2014 年 3 月 11 日
I have not yet understood it big picture; the summary use cases.
You have two GUIs
  • edit_time. Is there one per room. It has one push-button, Apply. A user edits the schedule and presses [Apply]. "NaN" what is that?
  • PROGRAM. Change Time and Change Room Name. "Select" is that not a better word than "Change".
There is one mat-file, ??_compare.mat, for each building?
The structure of the data will affect the execution time. "[...]like floor(x).room(y).date(i).slot(z)?" Tentatively, I would say
building(.).floor(.).room(.).sdn
building(.).floor(.).room(.).slot
where sdn is a row vector of "serial date number" or other time and slot i a array [2x12xlength(sdn)]. With Matlab one should not fragment data "too much". slot is that a period of time with minute resolution?
Before deciding on the data structure one should analyze how data will be accessed.
ericson
ericson 2014 年 3 月 12 日
  • Yes there is one per room, but the gui it opens is the smae, only the data that it loads in that gui changes. NaN is just the sample building name I assigned to it.
  • Ok, I'll change the name
there is a one mat file for each ??_compare.mat
I don't get it, where will I use the sdn for? is it different from the slot?
slot also includes the minute. I compare the time by combining the hr and min.
ex.
a1mon1_hr=13;
a1mon1_min=02;
evalc(sprintf('a1mon1=%d%d',a1mon1_hr,a1mon1_min);
This function is being processed in the edit_time gui where the data is being stored in the ??_compare.mat
per isakson
per isakson 2014 年 3 月 12 日
編集済み: per isakson 2014 年 3 月 12 日
I have problems understanding how this tool will be used; which the requirements are. I think of it as a "booking system". Or is it rather a system to report whether rooms are actually used.
What does the color of the room button exactly mean? Does a red button mean that the room is being used "at the moment", i.e 10:31:54.
What happens when the [Apply]-button is pressed?
Each "slot" has a starting and an ending point in time together with a check-box according to the GUI. Why call it "slot". "time slot" make me think of predefined non-overlapping periods of time.
I cannot understand why there is a problem with "computer load", which indicates that I don't understand the requirements.
ericson
ericson 2014 年 3 月 12 日
It is both a reservation system and a system that reports whether the room is actually used or not.
In the PROGRAM gui, when you press the push button, the schedule will appear in the right side, and there you will see a check box. The check box is used for the reservation system. If the check box in a specific time slot is not checked, then even if the current time is within the time slot, the color of the push button will not turn to red (meaning it is vacant).
Red button means it is currently being used. and it will be green if it is not within the time slot
I called it a slot because there should be no overlapping time in the 12 slots available per day in each room.
I called it a computer load because my main problem right now is that I don't have an efficient code which will compare the current time to every time slot in every room (which is 120). My gui hangs in a way that the real-time clock in the upper right of the PROGRAM gui is not working anymore.
per isakson
per isakson 2014 年 3 月 12 日
編集済み: per isakson 2014 年 3 月 12 日
In the edit_time GUI, is the user supposed to fill in time in the edit boxes, which show the value "00:00-00:00"? Or are the start end end times of the slots predefined?
What happens when the [Apply]-button of the edit_time GUI is pressed?
ericson
ericson 2014 年 3 月 13 日
The edit_time gui will not open until a specific room push button is pressed. The change time button is the one that will open the edit_time gui. The edit_time gui knows that it is for the floor 1 room 25 by using the setappdata function when a specific push button of a room is pressed.
The user then will decide if he wants to put a time slot on a specific day. (i.e. the user will only want to have 5 time slots from monday to saturday and there will be no time slot taken when sunday. After editing the time slots the way the room schedule is supposed to be, the apply button will then be pressed which will then overwrite the existing mat files. (i.e. if floor 1 room 3, then the files that will be overwritten are a3.mat and a3_compare.mat)
per isakson
per isakson 2014 年 3 月 13 日
It's more effective to have discussions like this one in front of a white-board. "I don't get it, where will I use the sdn for? is it different from the slot?". My problem is that I don't get it (appropriate smiley).
I understand that
  • the edit_time GUI is only used from the PROGRAM GUI. (I did imagine some kind distributed system where the user of the room used edit_time to book a room.)
  • there is a way to select a specific building, which is not obvious from the screen clips.
  • "[...] editing the time slots [...]". I interpret, for each time slot the user types start and end time and finally "checks the box". If so, there is a need for asserting that time slots do not overlap. Are old definitions of time slots saved?
  • rooms can only be booked for the current week - not for the whole semester.
What is the reason you have so many different mat-files? (In data base design something called ACID is important.) Saving and loading data of these files are likely what make your system choke.
I have read the thread and highlighted following:
"after all the variables are generated, it will then be compared to the time. If the current time is in between the time specified of the user having the same date (mon, tues, etc.), then the background color of the push button will change"
"[...] continuously being compared"
"let say var=12:00 and fvar=13:00, the current time should be in between of it so that the background color of the pushbutton will turn to red."
"in one building there are 240 mat files, 2 mat files per room(1 for the schedule and 1 for the comparing of the schedule)"
.
The simple solution to your efficiency problem might be
The not so simple solution is to keep the GUIs and rewrite most of the code.
ericson
ericson 2014 年 3 月 13 日
The edit_time is not used by the user, it is used only by the admin.
for the edit_time gui
  • there the admin will input all the time slots that is available within that day, that means that the user who's going to serve a room will not choose the time itself, but will only pick on time slots assigned by the admin
  • The purpose of the check box is, when the it is checked, the user will can't check or uncheck that time slot in the program gui, it is always checked
For the PROGRAM gui
  • The check box on the right side is the one that determines if the time slots that is not checked in the edit_time is going to be used. (i.e. I checked slots 1-5 and left 6-12 unchecked, it means that in order for the room to be reserved within the time in between slots 6-12, the check box in the PROGRAM gui must be checked)
based on what you said, I'm currently compressing the mat files that I'm using. I'm trying to make it into 1 mat file now where all the data are stored in it.
I think efficiency is not the right term anymore. I think the problem is because I have a continuous loop (loop inside while(1)). I need my real time clock in my gui to run smoothly and at the same time, the current time should be compared to all the slots and to all the rooms. I can't think of an efficient code where I can compare the current time to all the time slots, all I can think of is using thousands of if statement.
per isakson
per isakson 2014 年 3 月 13 日
編集済み: per isakson 2014 年 3 月 13 日
"I can't think of an efficient code [...]" I'm convinced a comparison can be done once every second. However, that requires a different way of keeping the "slot information".
Despite that I asked several times you have not exactly defined "time slot". Without such definitions it is hard to contribute. The screen copies you uploaded shows 00:00-00:00. When and by who are the actual values set? Which constraints on the values are there.
ericson
ericson 2014 年 3 月 13 日
In what way should I do it so that I can compare it every second? Right now this is my main problem.
Time slot is the set of start time and end time (i.e. 12:00-13:00). The 12 time slots per day should not overlap each other. Assuming the program will be set for the first time, all time slots are set to 00:00-00:00. After they are edited to the time ranges that you want, just click the apply button and it will be saved to the mat file. I already have a code which updates the mat file loaded in the PROGRAM gui when a change in the time in any room happened.
Sorry if I still didn't give you the answer that you want. I don't actually understand your question "When and by who are the actual values set? Which constraints on the values are there."
per isakson
per isakson 2014 年 3 月 13 日
編集済み: per isakson 2014 年 3 月 13 日
I've added a timing experiment to the answer above. The structure Building may
  • be made persistent in the main function.
  • saved to a version '-v6' mat-file as needed
I represent time with floating-point integer to avoid rounding errors. ssn stands for serial second number.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeGit in MATLAB についてさらに検索

タグ

質問済み:

2014 年 3 月 10 日

編集済み:

2014 年 3 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by