Im new in matlab, Im trying to call a function inside a matlab GUI but I keep getting an error. Can you please help.
function PushMe_Callback(hObject, eventdata, handles)
set(handles.output_line,'String','I Got here');
Get_Length(Note_Vector,output,Counter,Total_num)
%------------------------------------------------------------
function Get_Length(Note_Vector,output,Counter,Total_num)
output = [ ];
while (1)
T = Total_num - Counter;
for j=1:T
[xml_input]=Get_Note(Note_Vector(j));
output = [output xml_input];
end
end
You have to get your data from the gui data. You can retrieve these data from the function "guidata".
In fact what happens, is that you define a variable in the "main function" but you cannot access this one.
What you should do is to add this variable in the handles in the "GUI_OpeningFcn" function:
Note_Vector = [ ];
output = [ ];
Counter = 0
for i=1:Total_num
Note_Vector = [Note_Vector Note+Shift];
end
handles.Note_Vector = Note_Vector;
handles.GLoutput = output; %handles.output is already the handle of your GUI, don't change it.
handles.counter=counter;
handles.Total_num=Total_num;
(...)
guidata(hObject, handles);
then you can either retrieve these data in PushMe_Callback by simply writing handles.Note_Vector OR you can retrieve them by doing the following in Get_Length function
handles = guidata(handle). In that case you'll have to put in input the handle of your GUI
Get_Length(Note_Vector,output,Counter,Total_num,handles.output)
I let you check the matlab help file concerning guidata() if you want more information.
Related
I am using a postman to automate apis.
Now I am using following request , lets say :-
{
"customerId": "{{currentClientId}}"
}
Where clientid is a dynamic variable whose value is substituted dynamically as 1 , 2, 3,4 so on..
I call this request multiple times using setNextRequest call in this eg lets say 10.This is being done using a counter variable. I am initialising the counter in my previous request to 0 and using for loop with value as counter as 10 calling the request 10 times.There is no response in body just successful http code 204.
I want to store all these clientids coming in request into an environment Client array variable so I wrote a following pre-request script:-
counter = pm.environment.get("counter");
ClientArray = pm.environment.get("ClientArray");
ClientArray.push(pm.environment.get("currentClientId"));
pm.environment.set("ClientArray",ClientArray);
In Test Script, wrote following code :-
counter = pm.environment.get("counter");
if(counter<=10) {
console.log("hi");
postman.setNextRequest("Request");
counter++;
pm.environment.set("counter",counter);
console.log("Counter",counter);
}
The above scipts is throwing
TypeError | ClientArray.push is not a function.
Could someone please advice how to achieve this.
When you retrieve a value from an environment variable like you're doing:
ClientArray = pm.environment.get("ClientArray");
You're not getting an array, you're getting a string which is why you're getting that error. You need to treat the variable like a string, append the currentClientId much like you do for the counter. Something like:
var currentClientIds = pm.environment.get("ClientArray");
currentClientIds = currentClientIds + "," + currentClientId
When you're done appending i.e. out of your loop simply take the string and convert it to an array:
var currentClientIds = pm.environment.get("ClientArray");
var idsArr = curentClientIds.split(',');
I am trying to read a file within a controller and store some data in an object, but I cant manage to save it properly. Can anyone help? I am new in Groovy/Grails...
File generals = new File("C:/Grails/Grails-3.3.0/ggts/Test/data.txt")
def line = generals.readLines()
def date = new SetDate(params)
date.save()
date.title = ${line[0]}
date.location = ${line[1]}
date.description = ${line[2]}
date.name = ${line[3]}
date.email = ${line[4]}
date.save()
You may change ${line[0]} to "${line[0]}" and all things alike if you want to use string interpolation.
And as line is a list of String, change ${line[0]} to line[0] is also ok.
Could any one show me how i can add result of =importxml into an array inside google script. For example this the request inside google script :
=IMPORTXML("https://www.google.com/search?q=bmw&safe=off&tbs=qdr:d","//h3[#class='r']")
I want later iterate the array to look for specific String!
One option you can try is:
/* CODE FOR DEMONSTRATION PURPOSES */
function ImportXML2Array() {
var sheet = SpreadsheetApp.getActiveSheet(),
range,
values_array;
sheet.getRange("A1").setFormula('=IMPORTXML("https://www.google.com/search?q=bmw&safe=off&tbs=qdr:d"; "//h3[#class=\'r\']")');
range = sheet.getDataRange();
values_array = range.getValues();
range.clear();
Logger.log(values_array);
}
/* CODE FOR DEMONSTRATION PURPOSES */
I want to read a serail port every 0.1s and append the incoming data to an array, I can show the data this time but the array seems only store the newest data. Anyone can tell me why? Thanks.
Here is some code:
function wtsMat_OpeningFcn(hObject, eventdata, handles, varargin)
.....
%%tact is the array to store data
tact=ones(1,84);
handles.tact=tact;
% Update handles structure
guidata(hObject, handles);
Here is setting of scom
scom=serial(com_cur,'BaudRate',baud_curNum,'Parity','none','DataBits',8,'StopBits',1,...
'InputBufferSize',1000,...
'TimeOut',1,...
'TimerPeriod',0.1,...
'timerfcn',{#mycallback,handles});
fopen(scom);
handles.scom=scom;
guidata(hObject,handles);
here is mycallback function
function mycallback(scom,BytsAvailable,handles)
%start single frame acquisition
showData=ones(84,1);
showWin=ones(14,6);
%%get previous data from handles
tact=handles.tact;
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'20';'01';'00';'00';'8F';'83'])));
myData = fread(scom,183);%read raw data from sensor
for i=1:84
showData(i,1)=myData(13+i*2)*16*16+myData(12+i*2);
end
%%append to tact array
tact=[tact;showData'];
handles.tact=tact;
....
save tact when close the scom
function pb_close_Callback(hObject, eventdata, handles)
% hObject handle to pb_close (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
scom=handles.scom;
%stop acquising
fwrite(scom,uint8(hex2dec(['AA';'AA';'AA';'22';'00';'00';'0E';'76'])));
fclose(scom);
tact=handles.tact;
save('tact.mat','tact');
Try creating a buffer and saving the new data in the last position at every iteration
buf_len = 100;
index = 1:buf_len;
%Initialize array
arrayOfData = zeros(buf_len,1);
% get Data here. Let's say the new value is theta
arrayOfData = [ arrayOfData (2:end) ; theta ];
plot(index,Tdata,'r','LineWidth',2);
I'm new to F# and trying to dive in first and do a more formal introduction later. I have the following code:
type Person =
{
Id: int
Name: string
}
let GetPeople() =
//seq {
use conn = new SQLiteConnection(connectionString)
use cmd = new SQLiteCommand(sql, conn)
cmd.CommandType <- CommandType.Text
conn.Open()
use reader = cmd.ExecuteReader()
let mutable x = {Id = 1; Name = "Mary"; }
while reader.Read() do
let y = 0
// breakpoint here
x <- {
Id = unbox<int>(reader.["id"])
Name = unbox<string>(reader.["name"])
}
x
//}
let y = GetPeople()
I plan to replace the loop body with a yield statement and clean up the code. But right now I'm just trying to make sure the data access works by debugging the code and looking at the datareader. Currently I'm getting a System.InvalidCastException. When I put a breakpoint at the point indicated by the commented line above, and then type in the immediate windows reader["name"] I get a valid value from the database so I know it's connecting to the db ok. However if I try to put reader["name"] (as opposed to reader.["name"]) in the source file I get "This value is not a function and cannot be applied" message.
Why can I use reader["name"] in the immediate window but not in my fsharp code? How can I use string indexing with the reader?
Update
Following Jack P.'s advice I split out the code into separate lines and now I see where the error occurs:
let id = reader.["id"]
let id_unboxed = unbox id // <--- error on this line
id has the type object {long} according to the debugger.
Jack already answered the question regarding different syntax for indexing in F# and in the immediate window or watches, so I'll skip that.
In my experience, the most common reason for getting System.InvalidCastException when reading data from a database is that the value returned by reader.["xyz"] is actually DbNull.Value instead of an actual string or integer. Casting DbNull.Value to integer or string will fail (because it is a special value), so if you're working with nullable columns, you need to check this explicitly:
let name = reader.["name"]
let name_unboxed : string =
if name = DbNull.Value then null else unbox name
You can make the code nicer by defining the ? operator which allows you to write reader?name to perform the lookup. If you're dealing with nulls you can also use reader?name defaultValue with the following definition:
let (?) (reader:IDataReader) (name:string) (def:'R) : 'R =
let v = reader.[name]
if Object.Equals(v, DBNull.Value) then def
else unbox v
The code then becomes:
let name = reader?name null
let id = reader?id -1
This should also simplify debugging as you can step into the implementation of ? and see what is going on.
You can use reader["name"] in the immediate window because the immediate window uses C# syntax, not F# syntax.
One thing to note: since F# is much more concise than C#, there can be a lot going on within a single line. In other words, setting a breakpoint on the line may not help you narrow down the problem. In those cases, I normally "expand" the expression into multiple let-bindings on multiple lines; doing this makes it easier to step through the expression and find the cause of the problem (at which point, you can just make the change to your original one-liner).
What happens if you pull the item accesses and unbox calls out into their own let-bindings? For example:
while reader.Read() do
let y = 0
// breakpoint here
let id = reader.["id"]
let id_unboxed : int = unbox id
let name = reader.["name"]
let name_unboxed : string = unbox name
x <- { Id = id_unboxed; Name = name_unboxed; }
x