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.
Related
TL;DR: How should custom simulation runs be managed in Matlab? Detailed Questions at the end.
I am working with matlab where i created some code to check the influence of various parameters on a simulated system. It has a lot of inputs and outputs but a MWE would be:
function [number2x,next_letter] = test(number, letter)
number2x = number * 2;
next_letter = letter + 1;
disp(['next letter is ' next_letter])
disp(['number times 2 is ' num2str(number2x)])
end
This works if this is all there is to test. However with time multiple new inputs and outputs had to be added. Also because of the growing number of paramters that have been test some sort of log had to be created:
xlswrite('testfile.xlsx',[num2str(number), letter,num2str(number2x),next_letter],'append');
Also because the calculation takes a few hours and should run over night multiple parameter sets had to be started at one point. This is easily done with [x1,y1] = test(1,'a');[x2,y2] = test(2,'b'); in one line or adding new tasks while the old still run. However this way you can't keep track on how many are still open.
So in total I need some sort of testing framework, that can keep up with changeging inpus and outputs, keeps track on already doen calculations and ideally also handles the open runs.
I feel like i can't be the only one who faces this issue, in fact I think so many people face this issue that Mathworks would already came up with a solution.
For Simulink this has been done in form of a Simluationmanager, but for Matlab functions the closest thing i found is the Testing framework (example below) which seems to be rather for software development and debugging and not at all for what i am trying. And somepoint there seem to be 3rd party solutions but they are no longer continued in favor of this Testing framework.
function solutions = sampleTest
solutions = functiontests({#paramtertest});
end
function paramtertest(vargin)
test(1,'a');
test(2,'b');
end
function [number2x,next_letter] = test(number, letter)
number2x = number * 2;
next_letter = letter + 1;
disp(['next letter is ' next_letter])
disp(['number times 2 is ' num2str(number2x)])
xlswrite('testfile.xlsx',[num2str(number), letter,num2str(number2x),next_letter],'append');
end
Alternatively I could create my test as a class, create an interface similar to the Simulationmanager, create numerous functions for managing inputs and outputs and visualize the progress and then spawn multiple instances of if i want to set up a new set of parameters while already running a simulation. Possible, yet a lot of work that does not involve the simulation directly.
In total following questions arise:
Is there a build in Matlab function for managing simulations that i totally missed so far?
Can the the Testing framework be used for this purpose?
Is there already some Framework (not from Mathworks) that can handle this?
If i create my own class, could multiple instances run individually and keep track of their own progress? And would those be handled simultaniously or would matlab end up running the in the order they started?
I know this question is somewhat in the off-topic: recommend or find a tool, library or favorite off-site resource area. If you feel it is too much so, please focus on the last question.
Thank you!
I've done similar tests using GUI elements. Basic part of simulation was inside while loop, for example in your case:
iter = 0;
iter_max = 5; %the number of your times, you will call script
accu_step = 2; %the accuracy of stored data
Alphabet = 'abcdefghijklmnopqrstuvwxyz'
while iter < iter_max
iter = iter+1;
[x1,y1] = test(i,Alphabet(i));
end
Now you should create a handle to progress bar inside your computation script. It will show you both on which step you are, and the progress of current step.
global h;
global iter_opt;
if isempty(h)
h=waitbar(0,'Solving...');
else
waitbar(t/t_end,h,sprintf('Solving... current step is:%d',iter));
end
You didn't specified which function you use, if it is for example time-depended the above t/t_end example is an estimation of current progress.
The solving of result also require to be changed on every execution of loop, for example:
global iter;
i_line = (t_end/accu_step+2)*(iter-1);
xlswrite('results.xlsx',{'ITERATION ', iter},sheet,strcat('A',num2str(i_line+5)))
xlswrite('results.xlsx',results_matrix(1:6),sheet,strcat('D',num2str(i_line+5)))
The above example were also with assumption that your results are time-related, so you store data every 2 units of time (day, hours, min, what you need), from t_0 to t_end, with additional 2 rows of separation, between steps. The number of columns is just exemplary, you can adjust it to your needs.
After the calculation is done, you can close waitbar with:
global h
close(h)
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.
I have a .m file (script) that is controlling a real-time robot.
What i do within this file is:
1- find a trajectory
2-infinite loop:
read from robot
update robot
plot some stuff (basically I'm drawing a new point in each iteration that represents the position of the robot in a previously opened map, it's updating the map)
end of loop
What I want to do is to create a GUI that allows me to make the plots and see some values that the robot returns at the same time, in real time.
From what I read, MATLAB can't run both a script and a GUI at the same time.. I can make it plot in real time in the GUI but I can't seem to be able to update the values returned by the robot in text boxes in GUI..
Do I have to put it all in the same file or is there a way for the GUI and the script to work in separate files?
Thank you in advance!
MATLAB has not trouble running both. I don't know where you read that but it's not true; MATLAB is not the best tool to solve this problem, but it can do it.
First, I'm going to frame your problem in code, to make it easier to solve. Your question is vague and general, so my response has to be be general as well. I'm making some assumption about your function structure, but it really should look a lot like this:
endflag = 0;
while ~endflag
robotData = getRobotData(robotHandel);
derivedData = doStuffWithData(robotData);
updateRobot(derivedData);
showData(robotData, derivedData)
endflag = checkEndFlag(robotData, derivedData)
end
So, your problem is the showData functionality. What it should do, is determine which values need to be displayed from it's inputs, and pass those to your GUI. Like so:
function showData(robotData, derivedData)
guiInputData = dataParser(robotData, derivedData)
YourGUIFunctionName(guiInputData)
end
The GUI function should then build itself using those inputs. Any GUI function working that way, will do what you want it to. If you want a more specific solution, you need to give me more specific information about your problem. Good luck, I hope this helps.
Pretty simple, and I thought I knew what I was doing, but apparently not. Anyways.
I need to find the first element in a vector that is less than a specific value. Here is the code that I have been using:
t = 0:0.01:5;
u = ((2)*exp(-10.*t).*cos((4.*sqrt(6)).*t) + ((5)./sqrt(6)).*exp(-10.*t).*sin((4.*sqrt(6)).*t));
for a = 1:size(u)
if u(a) < (0.05)
disp(a)
break
end
end
The value that I'm trying to find is the first element less than 0.05, however, when I run my code, I don't get anything.
What could I be doing wrong?
Thanks!
#user2994291 has correctly pointed out where your loop based solution is going wrong (+1).
However I would also add that what you are trying to do can simply be accomplished by:
find(u < 0.05, 1, 'first')
Technically, the third input is not necessary - you could just use:
find(u < 0.05, 1)
However, I seem to recall reading at some point that find will work faster if you provide the third input.
The upper bound of your for loop is probably equal to 1.
In your case, u is a row vector (can't say 100% for sure in MATLAB as I only have access to GNU Octave right now), but calling size(u) will probably give back [1 501] as an answer. Your for-loop will select 1 as upper bound.
Try replacing size(u) with size(u,2) or, even better, with length(u). I get a = 24 as an answer.
Edit:
from your questions I assume you are a MATLAB beginner, therefore I strongly advise you to look into the built-in debugger (you can add breakpoints by clicking on the left vertical bar next to the desired line of code), this would have helped you identify the error with ease and will save you a lot of time in the future.
I am completely new to matlab and can't seem to get an if loop to work. For example if Ln > k , plot point i(n-1) to i(n). How would I automatically assign the correct row or column vectors to i(n)?
Here is a diagram of what I'm wanting
What I want to achieve is connect i(0) to i(1) to ... i(n-1) to i(n).
I also am a bit confused at which co-ordinate system to use? I thought it would be easy to use a polar co-ordinate system. Defining a distance and angle from point i(o) and then doing the same from point i(1) but from what I could find, it is necessary to convert back to a cartesian co-ordinate system.
Once I am comfortable with this section I am confident I can take on the next steps, and develop a full solution to my problem. If you are interested in what I am trying to achieve, here's a link
[PLEASE NOTE] In that question I linked to, I was told I made a mess of it. I'm sorry if this question is also not clear. I really have spent the time to make it as clear as possible. I find it hard to express myself sometimes.
For coordinate system, you can use complex numbers as a simple way of working within a 1-D matrix. Otherwise, I'm struggling to understand what you are trying to accomplish. You should at least try to show some code as we will be in a better position to guide you.
There are a bunch of ways you can execute your problem. without getting into details, you do the following:
n = 1
L(1) = ...
point(1) = ...
while (L(n) < k)
n = n+1;
L(n) = L(n-1)*sin(alpha)/sin(alpha+theta);
point(n) = ...
end
plot(point(1:n));