getting arrays into matrix

hi all, newbie here. working on an RT60 calculator in the app creator, and i'm trying to arrange several arrays into one matrix, but i keep getting an error message for it, and i can't seem to figure out why it doesn't work. here's a screenshot of it. please help

7 Comments

We are not given any information about the value of LeftWallMaterial
apologies for that. they should work from a drop down menu. here's the corresponding code:
%Convert dropdown options from string (text) to numbers
LeftWallMaterial = str2double(app.LeftwallmaterialDropDown.Value);
RightWallMaterial = str2double(app.RightwallmaterialDropDown.Value);
FrontWallMaterial = str2double(app.FrontwallmaterialDropDown.Value);
BackWallMaterial = str2double(app.BackwallmaterialDropDown.Value);
CeilingMaterial = str2double(app.CeilingmaterialDropDown.Value);
FloorMaterial = str2double(app.FloormaterialDropDown.Value);
SecondaryMaterialWall = str2double(app.SecondarywallmaterialDropDown.Value);
SecondaryMaterialCeiling = str2double(app.SecondaryceilingmaterialDropDown.Value);
SecondaryMaterialFloor = str2double(app.SecondaryfloormaterialDropDown.Value);
Walter Roberson
Walter Roberson on 12 May 2022
Edited: KSSV on 12 May 2022
I suggest that you configure Items Data Array for the list box.
I have no idea, what a "RT60 calculator is. I assume, this detail is not relevant.
Please post code as text, not as screenshot. This makes it much easier to type an answer.
The error message explains, that the contents of "LeftWallMaterial" is not a valid index. Use the debugger to check this. Type this in the command window:
dbstop if error
Run the code until it stios at the error. Then check the value of the index.
it's meant to be a reverberation calculator for rooms where you can input the dimensions, and the materials covering the surfaces, and based on the set coefficients, it will determine the reverberation times on 4 different frequencies.
not going to lie, this isn't my forte, but i need it for a uni assignment, so please be patient lol
I'll just copy in the full code to avoid related inconveniences in the future:
%width
RoomWidth = app.RoomwidthEditField.Value;
FloorWidth = app.FloorwidthEditField_2.Value;
CeilingWidth = app.CeilingwidthEditField.Value;
LeftWallWidth = app.LeftwallwidthEditField.Value;
RightWallWidth = app.BackwallwidthEditField.Value;
BackWallWidth = app.BackwallwidthEditField.Value;
FrontWallWidth = app.FrontwallwidthEditField.Value;
%length
RoomLength = app.RoomlengthEditField.Value;
FloorLength = app.FloorlengthEditField.Value;
CeilingLength = app.CeilinglengthEditField.Value;
LeftWallLength = app.LeftwalllengthEditField.Value;
RightWallLength = app.RightwalllengthEditField.Value;
BackWallLength = app.BackwalllengthEditField.Value;
FrontWallLength = app.FrontwalllengthEditField.Value;
%height
RoomHeight = app.RoomheightEditField.Value;
%calculate area of secondary material surface
AreaLWall2 = LeftWallLength * LeftWallWidth;
AreaRWall2 = RightWallLength * RightWallWidth;
AreaFWall2 = FrontWallWidth * FrontWallLength;
AreaBWall2 = BackWallWidth * BackWallLength;
AreaFloor2 = FloorLength * FloorWidth;
AreaCeiling2 = CeilingLength * CeilingWidth;
%Calculate area of room surfaces
AreaLWall = RoomLength * RoomHeight - AreaLWall2;
AreaRWall = RoomLength * RoomHeight - AreaRWall2;
AreaFWall = RoomWidth * RoomHeight - AreaFWall2;
AreaBWall = RoomWidth * RoomHeight - AreaBWall2;
AreaFloor = RoomLength * RoomWidth - AreaFloor2;
AreaCeiling = RoomLength * RoomWidth - AreaCeiling2;
%Convert dropdown options from string (text) to numbers
LeftWallMaterial = str2double(app.LeftwallmaterialDropDown.Value);
RightWallMaterial = str2double(app.RightwallmaterialDropDown.Value);
FrontWallMaterial = str2double(app.FrontwallmaterialDropDown.Value);
BackWallMaterial = str2double(app.BackwallmaterialDropDown.Value);
CeilingMaterial = str2double(app.CeilingmaterialDropDown.Value);
FloorMaterial = str2double(app.FloormaterialDropDown.Value);
SecondaryMaterialWall = str2double(app.SecondarywallmaterialDropDown.Value);
SecondaryMaterialCeiling = str2double(app.SecondaryceilingmaterialDropDown.Value);
SecondaryMaterialFloor = str2double(app.SecondaryfloormaterialDropDown.Value);
%Coefficients (125Hz 250Hz 500Hz 10000Hz 2000Hz 40000Hz)
Unpaintedconcrete = [0.01, 0.01, 0.02, 0.02, 0.02, 0.05];
Plasterboardonbattens = [0.31, 0.33, 0.14, 0.10, 0.10, 0.12]; %Plasterboard on 25mm battens
Parquetteonconcrete = [0.04, 0.04, 0.07, 0.06,0.06, 0.07];
Mediumcarpet = [0.50, 0.10, 0.30, 9.50, 0.65, 0.70]; %Medium pile carpet on sponge rubber underlay 10mm
Rockwoolpanel = [0.46, 0.93, 1, 1, 1, 1];
Drapery = [0.14, 0.35, 0.53, 0.75, 0.7, 0.6];
Glass = [0.30, 0.20, 0.10, 0.07, 0.05, 0.02];
%turn coefficient arrays into matrix
Matrix = [Unpaintedconcrete; Plasterboardonbattens; Parquetteonconcrete; Mediumcarpet; Rockwoolpanel; Drapery; Glass];
%get coefficient based on drop down option
Coefficient1 = Matrix(LeftWallMaterial,:);
Coefficient2 = Matrix(RightWallMaterial,:);
Coefficient3 = Matrix(FrontWallMaterial,:);
Coefficient4 = Matrix(BackWallMaterial,:);
Coefficient5 = Matrix(FloorMaterial,:);
Coefficient6 = Matrix(CeilingMaterial,:);
Coefficient7 = Matrix(SecondaryMaterialWall,:);
Coefficient8 = Matrix(SecondaryMaterialFloor,:);
Coefficient9 = Matrix(SecondaryMaterialCeiling,:);
%calculate absorption of each wall
AbsorptionLW = AreaLWall * Coefficient1;
AbsorptionRW = AreaRWall * Coefficient2;
AbsorptionFW = AreaFWall * Coefficient3;
AbsorptionBW = AreaBWall * Coefficient4;
%absorption of floor and ceiling
AbsorptionFloor = AreaFloor * Coefficient5 ;
AbsorptionCeiling = AreaCeiling * Coefficient6;
%calculate absorption secondary material
AbsorptionLW2 = AreaLWall2 * Coefficient7;
AbsorptionRW2 = AreaRWall2 * Coefficient7;
AbsorptionFW2 = AreaFWall2 * Coefficient7;
AbsorptionBW2 = AreaBWall2 * Coefficient7;
%absorption of floor and ceiling
AbsorptionFloor2 = AreaFloor2 * Coefficient8;
AbsorptionCeiling2 = AreaCeiling2 * Coefficient9;
%Total Absorption or Room
TotalAbsorptionRoom= AbsorptionLW + AbsorptionRW + AbsorptionFW + AbsorptionBW + AbsorptionFloor + AbsorptionCeiling + AbsorptionLW2 + AbsorptionRW2 + AbsorptionFW2 + AbsorptionBW2 + AbsorptionFloor2 + AbsorptionCeiling2;
%Sabine Room
RT60 = (0.162* Volume)./ TotalAbsorptionRoom;
app.HzEditField.Value = RT60(1);
app.HzEditField_2.Value = RT60(2);
app.HzEditField_3.Value = RT60(3);
app.HzEditField_4.Value = RT60(4);
app.HzEditField_5.Value = RT60(5);
app.HzEditField_6.Value = RT60(6);
the 'dbstop if error' came back with this message besides the original error message:
rethrow(exception);
In base workspace.
Jan
Jan on 12 May 2022
Edited: Jan on 12 May 2022
@Vilmos Popelyak: Of course it does. This is the purpose of the dbstop command. When Matlab stops at the error, you can examine the locvally used variables and find out, why the index is not a positive integer.
If this code is encapsulated in y TRY/CATCH block, this command stops Matlab more powerful:
dbstop if caught error

Sign in to comment.

Answers (1)

Binaya
Binaya on 2 Nov 2023
Hi Vilmos,
Based on your description, it seems that you want to address an error that occurs when executing the code, specifically the error message "Index in position 1 is invalid. Array indices must be positive integers or logical values."
Upon reviewing the provided code, it appears that the error arises from passing the value of the "LeftwallmaterialDropDown" as an index to an array using the function str2double().
To resolve this error, please ensure that the value selected in the "Leftwallmaterial" drop-down menu in the app designer is not a character, negative number, zero, or a fractional number. These types of values are not valid as array indices.
I hope this helps.
Regards
Binaya

Products

Release

R2021b

Asked:

on 12 May 2022

Answered:

on 2 Nov 2023

Community Treasure Hunt

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

Start Hunting!