Write a MATLAB script that a. starts with using the function struct to initializ
ID: 3554094 • Letter: W
Question
Write a MATLAB script that
a. starts with using the function struct to initialize a scalar structure named resistors with field names: resistor_name and resistor_value, where the two fields are empty,
b. Using input statements within a while loop to obtain resistor information to be placed in the respective fields. Use questions with yes/no answers to find out about entering resistors,
c. Display the table of resistor names and values
d. Use a debugging statement in the execution stage of your code.
Color
Digit value
Multiplier
Multiplied Out
Tolerance
Black
0
10
0
1
Brown
1
10
1
10
Red
2
10
2
100
Orange
3
10
3
1,000
Yellow
4
10
4
10000
Green
5
10
5
100,000
Blue
6
10
6
1,000,000
Violet
7
10
7
10,000,000
Gray
8
10
8
100,000,000
White
9
10
9
1,000,000,000
Gold
Color
Digit value
Multiplier
Multiplied Out
Tolerance
Black
0
10
0
1
Brown
1
10
1
10
Red
2
10
2
100
Orange
3
10
3
1,000
Yellow
4
10
4
10000
Green
5
10
5
100,000
Blue
6
10
6
1,000,000
Violet
7
10
7
10,000,000
Gray
8
10
8
100,000,000
White
9
10
9
1,000,000,000
Gold
Explanation / Answer
it will be helpful.
clear all, clc
resistors = struct('names',[],'values',[]); % initialize resistor structure
R_names = char([]); R_values = []; % initialize R vectors
capacitors = struct('names',[],'values',[]);
C_names = char([]); C_values = []; % initialize C vectors
disp('Enter a resistor name.')
terminate_text = char(['To terminate input, depress enter ', ...
'without first entering a name.']);
disp(terminate_text)
disp('Enter resistor value in number of K Ohms.')
R_in = input('Do you have resistor input, yes or no in single quotes? ');
if strcmpi(R_in,'yes') % case insensitive compare
R = 1; % used to terminate resistor input
while R
R_name = input('Enter a resistor name in single quotes: ');
if ~isempty(R_name) % check if there is input
R_value = input('Enter resistor value: ');
R_names = [R_names;R_name]; % add resistor name
R_values = [R_values;R_value];
else
R = 0;
end
end
endresistors.names = R_names; % assign resistor names to structure
resistors.values = R_values; % assign resistor values to structure
disp('Enter a capacitor name.')
disp(terminate_text)
disp('Enter capacitor values in number of uF.')
C_in = input('Do you have capacitor input, yes or no in single quotes? ');
if strcmpi(C_in,'yes')
C = 1; % used to terminate capacitor input
while C
C_name = input('Enter a capacitor name in single quotes: ');
if ~isempty(C_name)
C_value = input('Enter capacitor value: ');
C_names = char([C_names;C_name]);
C_values = [C_values;C_value];
else
C = 0;
end
end
endcapacitors.names = C_names;
capacitors.values = C_values;
disp(' ')
[N_Rs M] = size(resistors.names); % get number of resistors
if N_Rs > 0 % check if there was resistor input
disp('Resistors')
RNames = char(resistors.names);
RValues = resistors.values;
disp([RNames,repmat(' = ',N_Rs,1),num2str(RValues), ...
repmat(' K Ohms',N_Rs,1)]) % concatenate columns and display table
end
disp(' ')
[N_Cs M] = size(capacitors.names);
if N_Cs > 0
disp('Capacitors')
CNames = char(capacitors.names);
CValues = capacitors.values;
disp([CNames,repmat(' = ',N_Cs,1),num2str(CValues), ...
repmat(' uF',N_Cs,1)])
end
components = [resistors, capacitors]
Enter a resistor name.
To terminate input, depress enter without first entering a name.
Enter resistor value in number of K Ohms.
Do you have resistor input, yes or no in single quotes? 'yes'
Enter a resistor name in single quotes: 'R1'
Enter resistor value: 0.333
Enter a resistor name in single quotes: 'R2'
Enter resistor value: 2.2
Enter a resistor name in single quotes:
Enter a capacitor name.
To terminate input, depress enter without first entering a name.
Enter capacitor values in number of uF.
Do you have capacitor input, yes or no in single quotes? 'yes'
Enter a capacitor name in single quotes: 'C1'
Enter capacitor value: 0.1
Enter a capacitor name in single quotes: 'C2'
Enter capacitor value: 0.00047
Enter a capacitor name in single quotes: 'C3'
Enter capacitor value: 0.01
Enter a capacitor name in single quotes:
Resistors
R1 = 0.333 K Ohms
R2 = 2.2 K Ohms
Capacitors
C1 = 0.1 uF
C2 = 0.00047 uF
C3 = 0.01 uF
components =
1x2 struct array with fields:
names
values
>>
Enter a resistor name.
To terminate input, depress enter without first entering a name.
Enter resistor value in number of K Ohms.
Do you have resistor input, yes or no in single quotes? 'yes
Enter a resistor name in single quotes: 'R1'
Enter resistor value: 1
Enter a resistor name in single quotes:
Enter a capacitor name.
To terminate input, depress enter without first entering a name.
Enter capacitor values in number of uF.
Do you have capacitor input, yes or no in single quotes? 'no'
Resistors
R1 = 1 K Ohms
components =
1x2 struct array with fields:
names
values
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.