Can someone run this function and give the results as I have 2017 version and I believe there are some functions that work in latest versions

1 回表示 (過去 30 日間)
%% INPUT
%% Example truss
% Nodal x and y coordinates
nodeXY = [ 0 0; 10 5; 10 0; 20 8;20 0; 30 9;
30 0; 40 8; 40 0; 50 5; 50 0; 60 0];
% Each row represents an element - the columns are nodal indices
% E.g., 1 3 represents an element connecting node 1 (0, 0) and
% node 3 (10, 0) from the above list
elemNodes = [ 1 3; 3 5; 5 7; 7 9; 9 11; 11 12;
1 2; 2 4; 4 6; 6 8; 8 10; 10 12;
2 3; 4 5; 6 7; 8 9; 10 11; 2 5; 4 7; 7 8; 9 10];
% Function to plot and visualize the truss structure
plot_plane_truss(nodeXY, elemNodes)
%% THE FUNCTION
function [] = plot_plane_truss(nodeCrds, elemNodes)
% Check if the inputs have the correct dimensions
dims1 = size(nodeCrds);
dims2 = size(elemNodes);
if dims1(2) ~= 2 || dims2(2) ~= 2
error("Error: The arguments each should have two columns!")
end
% Separate x and y coordinates into different vectors
xCrds = nodeCrds(:, 1);
yCrds = nodeCrds(:, 2);
% Create a new figure which contains all the plots
figure
set(gcf, 'Position', [500, 500, 1200, 800]);
hold on
numElems = dims2(1); % Number of elements
elemMPXCrds = zeros(numElems, 1); % Stores element midpoint x-coordinates
elemMPYCrds = zeros(numElems, 1); % Stores element midpoint y-coordinates
% Loop over the input element connectivity matrix to plot each
% individual element separately
for e = 1:1:numElems
node1Idx = elemNodes(e, 1);
node2Idx = elemNodes(e, 2);
elemXCrds = [xCrds(node1Idx) xCrds(node2Idx)];
elemYCrds = [yCrds(node1Idx) yCrds(node2Idx)];
plot(elemXCrds, elemYCrds, '-k', 'LineWidth', 2);
elemMPXCrds(e) = mean(elemXCrds);
elemMPYCrds(e) = mean(elemYCrds);
end
% Node markers in red
nodeMarkerHandle = plot(xCrds, yCrds, 'ok', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
nodeMarkerHandle.Visible = 'off';
% Setting the aspect ratio and extent of the plots
xLen = max(xCrds) - min(xCrds);
yLen = max(yCrds) - min(yCrds);
pbaspect([xLen yLen 1])
offset = min(yLen, xLen)/2;
xlim([min(xCrds) - offset, max(xCrds) + offset]);
ylim([min(yCrds) - offset, max(yCrds) + offset]);
% Button to toggle the visibility of nodal position markers
tb1 = uicontrol;
tb1.Style = 'togglebutton';
tb1.String = {'Nodes On'};
tb1.Callback = @buttonCallback1;
tb1.Position = [100 200 100 20];
function buttonCallback1(src, event)
button_state = get(src, 'Value');
if button_state
nodeMarkerHandle.Visible = 'on';
tb1.String = {'Nodes Off'};
else
nodeMarkerHandle.Visible = 'off';
tb1.String = {'Nodes On'};
end
end
% Text labels for nodes
offset = 0.07*min(xLen, yLen);
nodeLblxCrds = xCrds - offset;
nodeLblyCrds = yCrds + offset;
numNodes = dims1(1);
nodeLbls = text(nodeLblxCrds, nodeLblyCrds, string([1:1:numNodes]), 'BackgroundColor', 'w', 'FontWeight', 'bold');
set(nodeLbls, 'Color', 'none');
% Button to toggle node labels
tb2 = uicontrol;
tb2.Style = 'togglebutton';
tb2.String = {'Node Labels On'};
tb2.Callback = @buttonCallback2;
tb2.Position = [220 200 100 20];
function buttonCallback2(src, event)
button_state = get(src, 'Value');
if button_state
set(nodeLbls, 'Color', 'b');
tb2.String = {'Node Labels Off'};
else
set(nodeLbls, 'Color', 'none');
tb2.String = {'Node Labels On'};
end
end
% Text labels for elements
offset = 0.*min(xLen, yLen);
elemLblxCrds = elemMPXCrds - offset;
elemLblyCrds = elemMPYCrds + offset;
elemLbls = text(elemLblxCrds, elemLblyCrds, string([1:1:numElems]), 'BackgroundColor', 'w', 'FontWeight', 'bold');
set(elemLbls, 'Color', 'none');
% Button to toggle element labels
tb3 = uicontrol;
tb3.Style = 'togglebutton';
tb3.String = {'Element Labels On'};
tb3.Callback = @buttonCallback3;
tb3.Position = [340 200 100 20];
function buttonCallback3(src, event)
button_state = get(src, 'Value');
if button_state
set(elemLbls, 'Color', 'r');
tb3.String = {'Element Labels Off'};
else
set(elemLbls, 'Color', 'none');
tb3.String = {'Element Labels On'};
end
end
end

採用された回答

Voss
Voss 2022 年 5 月 19 日
It appears to run ok in R2017b. I guess that means you have R2017a.
Here's the result:
openfig('untitled.fig');
If I had to guess which function is causing the problem, my first guess would be string. If that's the one, you can change
string([1:1:numNodes])
to
sprintfc('%d',1:numNodes)
and similarly for the other place where string is used.
If that's not the function causing the problem, how about you state what the problem is?
  4 件のコメント
Haseeb Hashim
Haseeb Hashim 2022 年 5 月 20 日
O my bad after correction of string function It ran but due to sizing issues It looked like that. Thanks man.. Again huge thanks to you

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2022 年 5 月 19 日
編集済み: David Hill 2022 年 5 月 19 日
Yes, code runs. All three buttons work properly.
nodeXY = [ 0 0; 10 5; 10 0; 20 8;20 0; 30 9;
30 0; 40 8; 40 0; 50 5; 50 0; 60 0];
% Each row represents an element - the columns are nodal indices
% E.g., 1 3 represents an element connecting node 1 (0, 0) and
% node 3 (10, 0) from the above list
elemNodes = [ 1 3; 3 5; 5 7; 7 9; 9 11; 11 12;
1 2; 2 4; 4 6; 6 8; 8 10; 10 12;
2 3; 4 5; 6 7; 8 9; 10 11; 2 5; 4 7; 7 8; 9 10];
% Function to plot and visualize the truss structure
plot_plane_truss(nodeXY, elemNodes)
Error using uicontrol
This functionality is not available on remote platforms.

Error in solution>plot_plane_truss (line 57)
tb1 = uicontrol;
function [] = plot_plane_truss(nodeCrds, elemNodes)
% Check if the inputs have the correct dimensions
dims1 = size(nodeCrds);
dims2 = size(elemNodes);
if dims1(2) ~= 2 || dims2(2) ~= 2
error("Error: The arguments each should have two columns!")
end
% Separate x and y coordinates into different vectors
xCrds = nodeCrds(:, 1);
yCrds = nodeCrds(:, 2);
% Create a new figure which contains all the plots
figure
set(gcf, 'Position', [500, 500, 1200, 800]);
hold on
numElems = dims2(1); % Number of elements
elemMPXCrds = zeros(numElems, 1); % Stores element midpoint x-coordinates
elemMPYCrds = zeros(numElems, 1); % Stores element midpoint y-coordinates
% Loop over the input element connectivity matrix to plot each
% individual element separately
for e = 1:1:numElems
node1Idx = elemNodes(e, 1);
node2Idx = elemNodes(e, 2);
elemXCrds = [xCrds(node1Idx) xCrds(node2Idx)];
elemYCrds = [yCrds(node1Idx) yCrds(node2Idx)];
plot(elemXCrds, elemYCrds, '-k', 'LineWidth', 2);
elemMPXCrds(e) = mean(elemXCrds);
elemMPYCrds(e) = mean(elemYCrds);
end
% Node markers in red
nodeMarkerHandle = plot(xCrds, yCrds, 'ok', 'MarkerSize', 6, 'MarkerFaceColor', 'r');
nodeMarkerHandle.Visible = 'off';
% Setting the aspect ratio and extent of the plots
xLen = max(xCrds) - min(xCrds);
yLen = max(yCrds) - min(yCrds);
pbaspect([xLen yLen 1])
offset = min(yLen, xLen)/2;
xlim([min(xCrds) - offset, max(xCrds) + offset]);
ylim([min(yCrds) - offset, max(yCrds) + offset]);
% Button to toggle the visibility of nodal position markers
tb1 = uicontrol;
tb1.Style = 'togglebutton';
tb1.String = {'Nodes On'};
tb1.Callback = @buttonCallback1;
tb1.Position = [100 200 100 20];
function buttonCallback1(src, event)
button_state = get(src, 'Value');
if button_state
nodeMarkerHandle.Visible = 'on';
tb1.String = {'Nodes Off'};
else
nodeMarkerHandle.Visible = 'off';
tb1.String = {'Nodes On'};
end
end
% Text labels for nodes
offset = 0.07*min(xLen, yLen);
nodeLblxCrds = xCrds - offset;
nodeLblyCrds = yCrds + offset;
numNodes = dims1(1);
nodeLbls = text(nodeLblxCrds, nodeLblyCrds, string([1:1:numNodes]), 'BackgroundColor', 'w', 'FontWeight', 'bold');
set(nodeLbls, 'Color', 'none');
% Button to toggle node labels
tb2 = uicontrol;
tb2.Style = 'togglebutton';
tb2.String = {'Node Labels On'};
tb2.Callback = @buttonCallback2;
tb2.Position = [220 200 100 20];
function buttonCallback2(src, event)
button_state = get(src, 'Value');
if button_state
set(nodeLbls, 'Color', 'b');
tb2.String = {'Node Labels Off'};
else
set(nodeLbls, 'Color', 'none');
tb2.String = {'Node Labels On'};
end
end
% Text labels for elements
offset = 0.*min(xLen, yLen);
elemLblxCrds = elemMPXCrds - offset;
elemLblyCrds = elemMPYCrds + offset;
elemLbls = text(elemLblxCrds, elemLblyCrds, string([1:1:numElems]), 'BackgroundColor', 'w', 'FontWeight', 'bold');
set(elemLbls, 'Color', 'none');
% Button to toggle element labels
tb3 = uicontrol;
tb3.Style = 'togglebutton';
tb3.String = {'Element Labels On'};
tb3.Callback = @buttonCallback3;
tb3.Position = [340 200 100 20];
function buttonCallback3(src, event)
button_state = get(src, 'Value');
if button_state
set(elemLbls, 'Color', 'r');
tb3.String = {'Element Labels Off'};
else
set(elemLbls, 'Color', 'none');
tb3.String = {'Element Labels On'};
end
end
end

カテゴリ

Help Center および File ExchangeStructural Analysis についてさらに検索

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by