insert array using graphical interface in matlab - arrays

I want to insert an array using a graphical interface, but I don't understand why I get these errors:
Error using waitfor
Undefined function or variable 'A'.
Error using waitfor
Error while evaluating uicontrol Callback
THE CODE:
function read()
clear all
clc
n=2;
b=50;
a=300;
B = nan(n);
S.fh = figure('units','pixels',...
'position',[500 500 500 500],...
'menubar','none',...
'numbertitle','off',...
'name','Matrix',...
'resize','off');
for i=1:n
for j=1:n
A(i,j) = uicontrol('style','edit','units','pixels',...
'position',[b a 50 50],'fontsize',20,'string','',...
'Callback', 'B(A == gco) = str2double(get(gco, ''string''));');
b = b+60;
end
b = 50;
a = a-60;
end
S.bb = uicontrol('style','push',...
'units','pixels',...
'position',[300 10 75 50],...
'fontsize',14,...
'string','Done',...
'callback','close');
waitfor(S.fh)
B

Instead of using callbacks for all the edit boxes separately, I recommend a single callback that reads all the values on the button. For instance:
function read()
clear all
clc
n=2;
b=50;
a=300;
% A = zeros(n);
S.fh = figure('units','pixels',...
'position',[500 500 500 500],...
'menubar','none',...
'numbertitle','off',...
'name','Matrix',...
'resize','off');
for i=1:n
for j=1:n
A(i,j) = uicontrol('style','edit','units','pixels',...
'position',[b a 50 50],'fontsize',20,'string','');
% no callback for the edit boxes
b = b+60;
end
b = 50;
a = a-60;
end
S.bb = uicontrol('style','push',...
'units','pixels',...
'position',[300 10 75 50],...
'fontsize',14,...
'string','Done',...
'callback',#(~,~)(readvalues(A,n)));
% callback that reads all the values in one run
% (and closes the figure as you wanted)
waitfor(S.fh)
function readvalues(A,n)
B = zeros(n);
for i=1:n
for j=1:n
B(i,j) = str2double(get(A(i,j), 'String'));
end
end
disp(B)
close

Related

love2D Error: Bad argument #2 to 'rectangle' (number expected, got nil)

I don't know what I'm doing wrong... Im getting the error in the player.draw() function. the bad argument that its talking about is in the love.graphics.rectangle() method. It's saying that v.x is a bad argument. But it should be working. both v.x and v.y should be working. Because its accessing fields of elements within the table or array right? Can someone tell me what I'm doing wrong and how to fix this? much appreciation! Here is my code:
require "scripts.player"
width = love.graphics.getWidth()
block = {}
block.width = 60
block.height = 10
block.speed = 150
block.timer = 0
block.timerLim = math.random(1,2)
block.spawnX = math.random(0, width - player.width)
function block.spawn(x,y)
table.insert(block, {x = x, y = y})
end
function block.move(dt)
for i,v in ipairs(block) do
v.y = v.y + block.speed * dt
end
end
function block.draw()
for i,v in ipairs(block) do
love.graphics.setColor(255,0,255)
love.graphics.rectangle("fill", v.x, v.y, block.width block.height)
end
end
function block.spawnHandler(dt)
block.timer = block.timer + dt
if block.timer > block.timerLim then
block.spawn(spawnX, -10)
block.timer = 0
block.timerLim = math.random(1,2)
block.spawnX = math.random(0, width - block.width)
end
end
-- Parent Functions --
function DRAW_BLOCK()
block.draw()
end
function UPDATE_BLOCK(dt)
block.move(dt)
block.spawnHandler(dt)
end
in function block.spawnHandler you meant block.spawnX instead of spawnX.
Since spawnX does not exist, its value is nil, which goes into the spawn function and gets set as the x value of the coordinate which then makes its way into rectangle and is the bad argument.
You can read more about that and how to prevent it from biting you again here: https://www.lua.org/pil/13.4.1.html
In short, lua is really weird, until you 'fix' it by making accesses to nonexistent variables throw errors instead of return nil, and about 800 other things like that. Once you 'fix' all those things, you have something which is merely weird and is not quite lua anymore.

Matlab- how can I change an array since it is seen as double

I need to iterate Newton-Raphson.The problem is:
For mmm=1:
1) If m=1 take c1=c1b and c2=1-c1 and do the loop for u1,2(i) and p1,2(i)
2)If m=2 take c1=c1+dc and c2=1-c1, and this time do the loop with new c1 and c2 for u1,2(i) and p1,2(i)
3) If m=3 take c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2)) and do the loop for new c1 and c2.
Then increase the iteration number: mmm=2 ;
mmm keeps count of the number of N-R iterations. The first iteration has mmm=1, the second mmm=2, etc. (This particular run only do 2 iterations).
sumint are inside of the integrals. 'c1, c2 are the camber effects, u1 u2 are the velocities, p1 p2 are pressures.
Relevant part of the code:
ii=101;
ub = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
ub{i} = zeros(1,ii);
ub{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=i*dx;
fikness = fik*sin(pi.*x);
ub{1}(i) = (c1b-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
ub{2}(i) = (c2b+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
mmm = 1;
c1 = c1b;
m = 1;
c2=1-c1;
u = cell(2, 1);
ini_cond = [0,0];
for i = 1:2;
u{i} = zeros(1,ii);
u{i}(:, ii) = ini_cond(i) * rand(1, 1);
end
for i=1:ii;
x=(i-1)*dx;
fikness = fik*sin(pi.*x);
u{1}(i) = (c1-H1D*(x-0.5)+AD/2.*(x-0.5).^2)./(H1-0.5*fikness-A*(x-0.5));
u{2}(i)= (c2+H1D*(x-0.5)-AD/2.*(x-0.5).^2)./(1.-H1+0.5*fikness+A*(x-0.5));
end
p = cell(2, 1);
q = cell(2, 1);
for i = 1:2;
p{i} = zeros(1,100);
q{i} = zeros(1,100);
end
p{1}(1) = 0.5*(1.-u{1}(1).^2);
q{1}(1) = 0;
p{2}(1) = 0.5*(1.-u{2}(1).^2);
q{2}(1) = 0;
for i = 2:ii;
q{1}(i) = q{1}(i-1)-dx*(u{1}(i-1)-ub{1}(i-1))./dt;
p{1}(i) = 0.5*(1.-u{1}(i).^2)+q{1}(i);
q{2}(i) = q{2}(i-1)-dx*(u{2}(i-1)-ub{2}(i-1))./dt;
p{2}(i) = 0.5*(1.-u{2}(i).^2)+q{2}(i);
end
st = zeros(2, 1);
st(1,:) = p{1}(100)-p{2}(100);
m = m+1;
if m==3;
c1=(c1*st(1)-(c1-dc)*st(2))/(st(1)-st(2));
c2=1-c1;
end
sumint = cell(2, 1);
for i = 1:2
sumint{i} = zeros(1,length(x));
end
sumint{1}(1) = 0.5*(p{2}(1)-p{1}(1));
sumint{2}(1) = 0.5*(p{2}(1)-p{1}(1)).*(-1/2);
for i = 2:100;
x=(i-1)*dx;
sumint{1}(i) = sumint{1}(i-1)+(p{2}(i)-p{1}(i));
sumint{2}(i) = sumint{2}(i-1)+(p{2}(i)-p{1}(i)).*(x-1/2);
end
The error is: ??? Attempted to access u.%cell(2); index out of bounds because numel(u.%cell)=1.
Error in ==> grab3_SmithWilson at 75 p{1}(i)=0.5*(1.-u{1}(i).^2)+q{1}(i);
You need to show us what you want to see. When I run the code you posted now, I find that first ub is printed as you have written, then each cell of ub is overwritten on each loop iteration. What I mean is that you are not putting values into the arrays that are stored in the cells, you are putting values in to the cells themselves. Are you sure that's what you want?
If you want to store the calculation in the elements of the arrays that are stored in the cells, the following will work:
for i=1:ii;
x=(i-1)*dx;
fikness=fik*sin(pi.*x);
ub{1}(i)=(c1b-H1D*(x-0.5)+AD/2*(x-0.5)^2)/(H1-0.5*fikness-A*(x-0.5));
ub{2}(i)=(c2b+H1D*(x-0.5)-AD/2*(x-0.5)^2)/(1-H1+0.5*fikness+A*(x-0.5));
end
>> ub
ub =
[1x101 double]
[1x101 double]
This is why I suggested reading about accessing parts of cells. Really though, this is just a guess until you tell us what you want from your script.

Lua/LOVE indexing problems

I'm getting a very irritating error whenever I do anything like this with arrays. I have code that sets up the array in the love.load() function:
function iceToolsInit()
objectArray = {} --for object handling
objectArrayLocation = 0
end
and then code that allows for the creation of an object. It basically grabs all of the info about said object and plugs it into an array.
function createObject(x, y, renderimage) --used in the load function
--objectArray is set up in the init function
objectArrayLocation = objectArrayLocation + 1
objectArray[objectArrayLocation] = {}
objectArray[objectArrayLocation]["X"] = x
objectArray[objectArrayLocation]["Y"] = y
objectArray[objectArrayLocation]["renderimage"] =
love.graphics.newImage(renderimage)
end
After this, an update function reads through the objectArray and renders the images accordingly:
function refreshObjects() --made for the update function
arrayLength = #objectArray
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
renderX = objectArray[arraySearch]["X"]
renderY = objectArray[arraySearch]["Y"]
renderimage = objectArray[arraySearch]["renderimage"]
if movingLeft == true then --rotation for rightfacing images
renderRotation = 120
else
renderRotation = 0
end
love.graphics.draw(renderimage, renderX, renderY, renderRotation)
end
end
I of course clipped some unneeded code (just extra parameters in the array such as width and height) but you get the gist. When I set up this code to make one object and render it, I get this error:
attempt to index '?' (a nil value)
the line it points to is this line:
renderX = objectArray[arraySearch]["X"]
Does anyone know what's wrong here, and how I could prevent it in the future? I really need help with this.
It's off-by-one error:
arraySearch = 0
while arraySearch <= arrayLength do
arraySearch = arraySearch + 1
You run through the loop arrayLength+1 number of times, going through indexes 1..arrayLength+1. You want to go through the loop only arrayLength number of times with indexes 1..arrayLength. The solution is to change the condition to arraySearch < arrayLength.
Another (more Lua-ly way) is to write this as:
for arraySearch = 1, #objectArray do
Even more Lua-ly way is to use ipairs and table.field reference instead of (table["field"]):
function refreshObjects()
for _, el in ipairs(objectArray) do
love.graphics.draw(el.renderimage, el.X, el.Y, movingLeft and 120 or 0)
end
end
objectArray and movingLeft should probably be passed as parameters...

Corona SDK array index is beyond array bounds, advice needed

I will try to as concise as possible with my issue.
Firstly, files are:
block.lua
base.lua
main.lua
In block.lua I create a block, add collision detection and a cleanup code.
In base.lua I create a base made up of 4 columns and 10 rows. 40 blocks in total.
In main.lua I create 4 bases made from the base.class.
All is working fine once the game begins.
I remove the bases and call them again on level 2.
They create themselves ok BUT
when the enemy is destroyed once again, and the bases are to be rebuilt, I get an:
array index 1 is beyond array bounds:1..1
-- all the way up to--
array index 800 is beyond array bounds:1..159
it will then create the bases and continue until the enemys are destroyed and do the same again starting at :
array index 800 is beyond array bounds:1..159
-- all the way up to--
array index 4000 is beyond array bounds:1..159
The terminal points me at block.lua line 23
blockGroup:insert(blockNum,self.block)
Now I cant see anything wrong in the class, I have looked and googled for hours but all to no avail.
I would really appreciate a helping hand to guide me here please.
I have tried rewriting the "cleanup" etc but no joy.
I left a few commented out bits in there and removed some of the irrelevant stuff.
I post below the relevant code:
--MAIN.LUA--
function gameOver()
Runtime:removeEventListener("enterFrame", onEnterFrame)
Runtime:removeEventListener("enterFrame", movePlayer)
layers:removeSelf()
layers = nil
enemyCount = 0
for i = 1,#allEnemys do
timer.cancel(allEnemys[i].clock)
Runtime:removeEventListener( "enterFrame", allEnemys[i] )
display.remove(allEnemys[i].image)
allEnemys[i].image=nil
end
allEnemys=nil
cleanupBlocks()
end
----------------------------------------------------------------------
-- LEVEL UP --
----------------------------------------------------------------------
function levelUp(level)
enemyCount = 0
local enemys = require("modules.enemy")
if allEnemys ~= nil then
for i = 1,#allEnemys do
timer.cancel(allEnemys[i].clock)
Runtime:removeEventListener( "enterFrame", allEnemys[i] )
display.remove(allEnemys[i].image)
allEnemys[i].image=nil
end
end
allEnemys=nil
cleanupBlocks()
levels()
end
----------------------------------------------------------------------
-- LEVELS --
----------------------------------------------------------------------
function levels(level)
function createInvader(x, y, row)
for j = 1, 2 do
for i = 1, 2 do
if allEnemys == nil then
allEnemys = {}
else
enemysCount=#allEnemys
end
allEnemys[#allEnemys + 1] = enemys:new()
allEnemys[#allEnemys ]:init(i * 60, j * 70 + 70,j)
allEnemys[#allEnemys ]:start()
end
end
end
createInvader()
--[[function createBases1()
local base = require("modules.base")
for i = 1, 4 do
base:new()
base:init(i * 180 - 130, 850)
end
end ]]--
createBases()
end
--BLOCK.LUA--
local block = {}
local block_mt = { __index = block}
local scene = scene
local blockGroup = display.newGroup()
local blockNum = 0
function block:new() -- constructor
local group = {}
return setmetatable( group, block_mt )
end
function block:init(xloc,yloc) --initializer
-- Create attributes
self.block = display.newRect( xloc,yloc,10,10)
self.block:setFillColor ( 2, 255, 14 )
blockNum = blockNum + 1
blockGroup:insert(blockNum,self.block)
local blockCollisionFilter = { categoryBits = 128, maskBits = 387}
physics.addBody( self.block, "static", {filter = blockCollisionFilter})
self.count = 1
end
function cleanupBlocks()
--[[ print(blockNum, blockGroup.numChildren)
for i=1,blockGroup.numChildren do
blockGroup[1]:removeSelf()
blockGroup[1] = nil
end ]]--
print(blockNum, blockGroup.numChildren)
while blockGroup.numChildren>0 do
display.remove(blockGroup[1])
blockGroup[1]=nil
end
end
function block:start()
--- Create Listeneres
self.block:addEventListener( "collision", self )
scene:addEventListener('base_block_event', self)
end
return block
--BASE.LUA--
local base = {}
local base_mt = { __index = base}
local scene = scene
local block = require("modules.block")
function base:new() -- constructor
local group = {}
return setmetatable( group, base_mt )
end
function base:init(xloc, yloc) --initializer
-- Create attributes
local base
for j = 1, 4 do
for i = 1, 10 do
base = block:new()
base:init(xloc+i * 10,yloc+j * 10)
base:start()
end
end
end
return base
I see you use
blockNum = blockNum + 1
blockGroup:insert(blockNum,self.block)
Try to use
blockGroup:insert(self.block)
just to see if you still get that error.

Appending Int to an Array Matlab

I am using an API to get real data times of trains and am trying to get the closest train time to a user entered time and then display that train time, and the next 4 granted the trains are running. I am reading in the information and the code goes through what its supposed to do but when I look at the array its a bunch of [] brackets in 7 cells instead of the calculated numbers. Any suggestions? Code is below with the API
TEST VALUES:
requestStationSelected = 'University%20City' and requestEndStation = 'Roslyn'
%this is the API link for the live data from Septa this will get 30
%results and see which time is closer to the user entered time
requestInfoSeptaLive = ['http://www3.septa.org/hackathon/NextToArrive/' requestStationSelected '/' requestEndStation '/30'];
%Again tries to get the information and if there is a failure it will give
%a probable cause and terminate the program
try
getInfoSeptaLive = urlread(requestInfoSeptaLive);
catch
if getInfoSeptaLive ~= '[]'
disp...
('Either the arrival/depart stations dont quite match up or theres a server error. Try again.');
return;
else
disp('Unable to fetch the information from Septa, please try again')
return;
end
end
%parses the information returned from the Live API
dataReturnedFromLiveAPI = parse_json(getInfoSeptaLive);
dataReturnedFromLiveAPI = dataReturnedFromLiveAPI{1};
%gets the size of the API in case there are no trains running
sizeOfDataNoTrains = size(dataReturnedFromLiveAPI, 1);
sizeOfData = size(dataReturnedFromLiveAPI, 2);
counter = 0;
for i = 1:sizeOfData
scanForClosestTime = dataReturnedFromLiveAPI{1,i}.orig_departure_time;
trainTimeGivenH = sscanf(scanForClosestTime, '%i');
findColonTrain = strfind(scanForClosestTime, ':');
trainTimeGivenMStr = scanForClosestTime(findColonTrain+1:4);
trainTimeGivenM = int32(str2num(trainTimeGivenMStr));
trainDepartTimeM = (trainTimeGivenH(1,1) * 60) + (trainTimeGivenM);
differenceBetweenTimes = trainDepartTimeM - userEnteredMins;
if trainDepartTimeM < userEnteredMins
differenceBetweenTimes = userEnteredMins - trainDepartTimeM;
end
stopAtEndOfData = sizeOfData;
goodTimeFrame = 60;
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
if(differenceBetweenTimes < 60)
if (counter < 5)
closestTime{i} = scanForClosestTime;
storeTheDifference{i} = differenceBetweenTimes;
counter = counter + 1;
end
end
end
You assign your cell arrays inside the for loop:
for i = 1:sizeOfData
...
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
...
end
This means that you turn both of them into an array of {[],[],[],[],[]...} on every iteration of the loop - so unless the last iteration has a valid "closest Time" in it, your cell array will be all empty arrays - and if it does, all but the last element will still be [].
To fix this, move the two lines to before the start of the for loop.
The second problem seems to be the indexing of the arrays where you store the results. If you only want five results, I am assuming you want to store them in elements 1 - 5 of your array, and not in "just any" locations. I would change the code to
if (counter < 5)
counter = counter + 1;
closestTime{counter} = scanForClosestTime;
storeTheDifference{counter} = differenceBetweenTimes;
end
But maybe I misinterpreted how you want to handle that?
Unrelated to your question, you might want to take a look at the line
trainTimeGivenMStr = scanForClosestTime(findColonTrain+1:4);
It is quite possible that this is not what you intended to do - looking at an example of the response, I found the string "orig_departure_time":"11:57PM". I expect that findColonTrain == 3, so that the above line becomes
trainTimeGivenMStr = scanForClosestTime(4:4);
just a single character. Perhaps you meant
trainTimeGivenMStr = scanForClosestTime(findColonTrain+(1:4));
which would turn into
trainTimeGivenMStr = scanForClosestTime(4:7);
so that
trainTimeGivenMStr = '57PM';
I hope these three things help you get it all working!
EDIT: had a chance to run your code this morning - discovered a number of other problems. I include below an annotated "working" code: the biggest problem was most likely that you were not handling AM/PM in your code. Note that I used a different json parser - this changed a couple of lines very slightly. I'm sure you can put it back together to work the way you want. This returned valid data in all cells.
dataReturnedFromLiveAPI = loadjson(getInfoSeptaLive);
% next line not needed - loadjson returns struct array, not cell array
%dataReturnedFromLiveAPI = dataReturnedFromLiveAPI{1};
%gets the size of the API in case there are no trains running
sizeOfDataNoTrains = size(dataReturnedFromLiveAPI, 1);
sizeOfData = size(dataReturnedFromLiveAPI, 2);
counter = 0;
stopAtEndOfData = sizeOfData;
closestTime = cell(1, stopAtEndOfData);
storeTheDifference = cell(1, stopAtEndOfData);
userEnteredMins = 12*60+30; % looking for a train around 12:30 pm
for ii = 1:sizeOfData
scanForClosestTime = dataReturnedFromLiveAPI(ii).orig_departure_time;
trainTimeGivenH = sscanf(scanForClosestTime, '%i');
% since we'll be considering AM/PM, have to set 12 = 0:
if (trainTimeGivenH == 12), trainTimeGivenH = 0; end
findColonTrain = strfind(scanForClosestTime, ':');
% change next line to get minutes plus AM/PM:
trainTimeGivenMStr = scanForClosestTime(findColonTrain+(1:4));
% look at just minutes:
trainTimeGivenM = int32(str2num(trainTimeGivenMStr(1:2)));
% adjust for AM/PM:
if(trainTimeGivenMStr(3:4)=='PM'), trainTimeGivenH = trainTimeGivenH+12; end;
% compute time in minutes:
trainDepartTimeM = (trainTimeGivenH * 60) + (trainTimeGivenM);
differenceBetweenTimes = trainDepartTimeM - userEnteredMins;
if trainDepartTimeM < userEnteredMins
differenceBetweenTimes = userEnteredMins - trainDepartTimeM;
end
% added a couple of lines to see what is happening:
fprintf(1, 'train %d: depart %s - in minutes this is %d vs user entered %d\n', ...
ii, scanForClosestTime, trainDepartTimeM, userEnteredMins);
goodTimeFrame = 60;
if(differenceBetweenTimes < 600)
if (counter < 10)
counter = counter + 1;
closestTime{counter} = scanForClosestTime;
storeTheDifference{counter} = differenceBetweenTimes;
end
end
end

Resources