How to copy data from a 1 x 1 double timeseries to a 1 D Lookup Table in Matlab - matlab

I have been asked to look at a Matlab project. I'll link sceenshots to clarify the problem. I need to create a 1 D Lookup table with the data from a 1 x 1 double timeseries from another model that has been supplied. One problem is that there are a lot of data points (12500). Is it possible to copy these points across without having to drag the mouse down over the whole 12500 points? Someone has actually tried this dragging the mouse over all the points method and said it didn't work anyway, but I don't really want to try it myself, as it would be way to cumbersome for my liking, even if it did work.
Here is an example of what the 1 x 1 double timeseries looks like (just using 5 points instead of 12500 for simplicity's sake):
Here is the model with the 1D look up table highlighted in blue at the left:
Here is what the 1-D lookup table looks like when opened:
Any insight appreciated.
I've worked out how to copy the data from the timeseries table (actually from the input to this, which is a 1 x 1 struct), but he problem is that the values have no commas between them and the 1-D lookup table requires commas.
Note that the problem has now been solved using Excel, although not by the method I was trying to make work in the question. An answer has been posted which may work, but I'm not sure if i will go to the trouble of attempting to implement it or not at this stage. However, if need be and all being well, I will either do that or delete the question.

You can import a look-up table object (Simulink.LookupTable-object) from both, the MATLAB workspace or directly from Excel.
If you want to automate it, it basically comes down to these two points:
"
Open the model containing the lookup table block and in the Modeling tab, select Model Settings.
In the Model Properties dialog box, in the Callbacks tab, click PostLoadFcn callback in the model callbacks list.
... the next time you open the model, Simulink® invokes the callback and imports the data.
"

After a little thought I feel like the answer is actually fairly trivial: it just can't be done. The input field of the look up table just can't hold 12500 points, most of which have 9 or so decimal places, so there's no way to do this.
This isn't to say there's no way to put the data into the model: this can be done using Excel.

Related

Reset Parameter Values to original value read from Excel sheet

I have parameters that's read its values from Excel spread sheet (Picture 1). However, I have a method that reduce the parameter values by x% (picture 2). I could reverse the calculation method in picture 2 which not going to help much.
Question:
I want to create a bottom that reset the parameters values to its original excel values. I am not sure how to create such a method? Appreciate any hints or ideas.
First of all, this is very redundant code you code put into a for-loop (select all your params, right click, then create a collection of Doubles).
Then, you can easily loop across all these again and simply divide them by interNo, getting you the original values.

How can I increase the speed of this xlsread for loop?

I have made a script which contains a for loop selecting columns from 533 different excel files and places them into matrices so that they can be compared, however the process is taking too long (it ran for 3 hours yesterday and wasn't even halfway through!!).
I know xlsread is naturally slow, but does anyone know how I can make my script run faster? The script is below, thanks!!
%Split the data into g's and h's
CRNum = 533; %Number of Carrington Rotation files
A(:,1) = xlsread('CR1643.xlsx','A:A'); % Set harmonic coefficient columns
A(:,2) = xlsread('CR1643.xlsx','B:B');
B(:,1) = xlsread('CR1643.xlsx','A:A');
B(:,2) = xlsread('CR1643.xlsx','B:B');
for k = 1:CRNum
textFileName = ['CR' num2str(k+1642) '.xlsx'];
A(:,k+2) = xlsread(textFileName,'C:C'); %for g
B(:,k+2) = xlsread(textFileName,'D:D'); %for h
end
Don't use xlsread if you want to go through a loop. because it opens excel and then closes excel server each time you call it, which is time consuming. instead before the loop use actxserver to open excel, do what you want and finally close actxserver after your loop. For a good example of using actxserver, search for "Read Spreadsheet Data Using Excel as Automation Server" in MATLAB help.
And also take a look at readtable which works faster than xlsread, but generates a table instead.
The most obvious improvement seems to be to load the files only partially if possible. However, if that is not an option, try whether it helps to only open each file once (read everything you need, and then assign it).
M(:,k+2) = xlsread(textFileName,'C:D');
Also check how much you are reading in each time, if you read in many rows in the first file, you may make the first dimension of A big, and then you will fill it each time you read a file?
As an extra: a small but simple improvment can be found at the start. Don't use 4 load statements, but use 1 and then assign variables based on the result.
As mentioned in this post, the easiest thing to change would be to set 'Basic' to true. This disables things like formulas and macros in Excel and allows you to read a simple table more quickly. For example, you can use:
xlsread('CR1643.xlsx','A:A', 'Basic', true)
This resulted in a decrease in load time from about 22 seconds to about 1 second for me when I tested it on a 11,000 by 7 Excel sheet.

How to forward test and plot?

Have received a lot of help. Will look for some more. Learning a lot by just going through the code.
I have thus far:
Loaded an xlsx file using xlsread function.
After this calculated the correlation coeff. Assigned it a variable(?).
Then used(Thanks #Robert P.):
BUYx = x(find(APC < -2.9079,1,'first'))
SELLy = y(find(APC < -2.9079,1,'first'))
BUYy = y(find(APC > 0.44,1,'first'))
SELLx = x(find(APC > 0.44,1,'first'))
To get the first value meeting a particular condition.
Now I want to forward test from where the first value is received till another condition is met. At the same time I would like to make a plot the results.
I am just wondering where to start looking to code/try this.
Please point me in the direction of the functions required. I think it has to loop through all the elements after receiving entry. Not sure which is the right way to go about it.
Thanks
Context:
I am trying to test a StatsArbitrage strategy. I was using excel for the work but I can't seem to get the right functionality.
Basically, when we have an entry(any set of the BUY SELL) I want the system to automatically start calculating Profit/Loss from the next data point and give an output to a new variable. Then I would plot it.
Then search for new entry and again start Profit/Loss calculation.

Matlab variable taking forever to save

I have a MATLAB variable that is a 3x6 cell array. One of the columns of the cell array holds at most 150-200 small RGB images, like 16x20 pixel size (again, at most). The rest of the columns are:
an equal number of labels that are strings of a 3 or 4 characters,
an image mask, which is about 350x200
3 integers
For some reason saving this object is taking a very long time, or at least for the size of the object. It has already been 10 minutes(which isn't too bad, but I plan on expanding the size of the object to hold several thousand of those small images) and MATLAB doesn't seem to be making any progress. In fact, when I open the containing directory of the variable, its size is cycling between 0 bytes to about 120kB. (i.e. it will increase to 120 in steps of 30 or 40 kB, then restart).
Is this normal behavior? Do MATLAB variables always take so long to save? What's going on here?
Mistake: I'm saving AllData, not my SVM variable. AllData has the same data as the SVM keeper, less the actual SVM itself and one integer.
What particular points of the code would be helpful to show for solving this? The code itself is a few hundred lines and broken up in several functions. What would be important to consider to troubleshoot this? When the variable is created? when it's saved? The way I create the smaller images?
Hate to be the noob who takes a picture of their desktop. but the machine I'm working has problems taking screenshots. Anyway, here it is
Alldata/curdata are just subsets of the 3x7 array... actually it's a 3x8, but the last is just an int.
Interesting side point: I interrupted the saving process and the variable seemed to save just fine. I trained a new svm on the saved data and it works just fine. I'd like to not do that in the future though.
Using whos:
Name Size Bytes Class Attributes
AllData 3x6 473300 cell
Image 240x352x3 253440 uint8
RESTOREDEFAULTPATH_EXECUTED 1x1 1 logical
SVMKeeper 3x8 2355638 cell
ans 3x6 892410 cell
curData 3x6 473300 cell
dirpath 1x67 134 char
im 240x352x3 1013760 single
s 1x1 892586 struct
Updates:
1.Does this always happen, or did you only do it once?
-It always happens
2.Does it take the same time when you save it to a different (local) drive?
-I will investigate this more when I get back to that computer
3.How long does it take to save a 500kb matrix to that folder?
-Almost instantaneous
4.And as asked above, what is the function call that you use?
-Code added below
(Image is a rgb image)
MaskedImage(:,:,1)=Image(:,:,1).*Mask;
MaskedImage(:,:,2)=Image(:,:,2).*Mask;
MaskedImage(:,:,3)=Image(:,:,3).*Mask;
MaskedImage=im2single(MaskedImage);
....
(I use some method to create a bounding box around my 16x20 image)
(this is in a loop of that occurs about 100-200 times)
Misfire=input('is this a misfire?','s');
if (strcmpi(Misfire,'yes'))
curImageReal=MaskedImage(j:j+Ybound,i:i+Xbound,:);
Training{curTrainingIndex}=curImageReal; %Training is a cell array of images
Labels{curTrainingIndex}='ncr';
curTrainingIndex=curTrainingIndex+1;
end
(the loop ends)...
SaveAndUpdate=input('Would you like to save this data?(say yes,definitely)','s');
undecided=1;
while(undecided)
if(strcmpi(SaveAndUpdate,'yes,definitely'))
AllData{curSVM,4}=Training;
AllData{curSVM,5}=Labels;
save(strcat(dirpath,'/',TrainingName),'AllData'); <---STUCK HERE
undecided=0;
else
DontSave=input('Im not going to save. Say YESNOSAVE to NOT SAVE','s')
if(strcmpi(DontSave,'yesnosave'))
undecided=0;
else
SaveAndUpdate=input('So... save? (say yes,definitely)','s');
end
end
end
It is a bit unclear if you are doing some custom file saving or not. If it is the first, I'm guessing that you have a really slow save loop going on, maybe some hardware issues. Try to save the data using MATLAB's save function:
tic
save('test.mat', 'AllData')
toc
if that works fine try to work your way from there e.g. saving one element at a time etc.
You can profile your code by using the profiler, open it with the command profile viewer and then type in the code, script or function that you want to profile in the input text field.
This isn't a great answer, but it seems that the problem was that I was saving the version of my image after I had converted it to a single. I don't know why this would cause such a dramatic slowdown (after removing this line of code it worked instantly) so if someone could edit my answer to shed more light on the situation, that would be appreciated.

Matlab GUI: referencing an existing object handle using a variable

I am currently working on my matlab final project for school. I consider myself fairly knowledgeable and proficient when it comes to programming.. but Matlab simply has TOO many oddities.
The fundamental question (realized this after finding the answer)! How can I use a variable in a call to a gui handle object
without the name of the variable being used instead of the value?
In other words: Use a variable in a field name (If I had known it was this simple I wouldn't of asked)
My project is building a simple rendition of the age old 'Battleship' game.
My issue: I currently 5 objects (axes) for the ship pieces. They are selected one at a time to be moved into another location(the grid). I am able to use the setpixelposition to move each object after a button click.
Right now under the button click, I have something like this
function btnPlaceShip_Callback(hObject, eventdata, handles)
%Store the current selected ship(passed from an onclick to a label)
ship = get(handles.lblSelectedShip,'string');
%I have tried everything I could think of, but basically I want to achieve the
%following
setpixelposition(handles.ship, [0 250 50 250])
%where the variable 'ship' contains the name of the object.
In other words, the var ship = 'shipAircraftCarrier', and..
setpixelposition(handles.shipAircraftCarrier, [0 250 50 250])
works!(sets the position of the specific ship indicated). Using the variable ship, however, matlab takes the string literally and not for its value. It would be extremely convenient to use the variable instead!
If anyone have any solutions, I would be grateful. I have scoured the web but perhaps I am missing some fundamental understanding of the Matlab GUI stuff - The matlab help documents are very non-descriptive and are not of much help.
As mentioned by others use dynamic fieldnames. Whilst not having the code to test it I believe that simply putting brackets around the ship will substitute the string in to the structure name, thus
setpixelposition(handles.(ship), [0 250 50 250])
Try to avoid the use of eval() if at all possible.
You can do this using the eval function, but you need to be careful about string injections:
setpixelposition(eval(strcat('handles.',ship)), [0 250 50 250])
You can use dynamic fieldnames or getfield. Field indexing using identifiers vs strings is quite similar in Matlab structs and Javascript objects.
Matlab:
fromId = handles.shipAircraftCarrier; %identifier
fromString = handles.('shipAircraftCarrier'); %string
Javascript:
var fromId = handles.shipAircraftCarrier; //identifier
var fromString = handles["shipAircraftCarrier"]; //string