Field reference for multiple structure elements that is followed by more reference blocks is an error. and undefined function histogram

5 views (last 30 days)
Field reference for multiple structure elements that is followed by more reference blocks is an error.
Error in (line 71)
scatter([vehicles.position(:,1)], [vehicles.position(:,2)], 'filled', 'MarkerFaceColor', 'b');
Undefined function 'histogram' for input arguments of type 'double'.
Error in (line 176)
histogram(vehicleProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'b',
'EdgeColor', 'w');
clear;
clc;
close all;
% Parameters
numVehicles = 50; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
numClusters = 5; % Number of clusters in the Things layer
cloudCapacity = 1000; % Cloud computing capacity
edgeCapacity = [100, 150, 200]; % Edge computing capacity
taskRate = 0.1; % Task generation rate per vehicle (tasks/second)
communicationDelayEdge = 1;% Communication delay for edge offloading (seconds)
communicationDelayCloud = 5;% Communication delay for cloud offloading (seconds)
distanceThreshold = 100; % Maximum distance for edge offloading (meters)
clusterRadius = 200; % Radius for clustering in the Things layer (meters)
vehicleComputingPower = 50;% Computing power of each vehicle (arbitrary units)
% Initialize vehicles
vehicles = struct('position', num2cell(rand(numVehicles, 2)*1000, 2), 'tasks', [], 'ip', '', 'computingPower', vehicleComputingPower);
% Initialize edge nodes
edges = struct('position', num2cell(rand(numEdges, 2)*1000, 2), 'tasks', [], 'capacity', edgeCapacity, 'ip', '');
% Cloud initialization
cloud = struct('tasks', [], 'capacity', cloudCapacity, 'ip', '8.8.8.8');
% Initialize clusters in the Things layer
clusters = struct('center', num2cell(rand(numClusters, 2)*1000, 2), 'members', []);
% Form clusters in the Things layer
for i = 1:numVehicles
minDist = inf;
nearestCluster = 0;
for j = 1:numClusters
dist = norm(vehicles(i).position - clusters(j).center);
if dist < minDist
minDist = dist;
nearestCluster = j;
end
end
clusters(nearestCluster).members = [clusters(nearestCluster).members, i];
end
% Assign IP addresses for vehicles in each cluster
for i = 1:numClusters
clusterSize = length(clusters(i).members);
clusterIPRange = 10 * (i - 1) + (1:clusterSize);
for j = 1:clusterSize
vehicles(clusters(i).members(j)).ip = ['192.168.', num2str(i), '.', num2str(clusterIPRange(j))];
end
end
% Initialize arrays to store processing times
vehicleProcessingTime = [];
edgeProcessingTime = [];
cloudProcessingTime = [];
% Simulation loop
figure;
for t = 1:100
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
clf; % Clear figure for each time step
% Plot vehicles
scatter([vehicles.position(:,1)], [vehicles.position(:,2)], 'filled', 'MarkerFaceColor', 'b');
hold on;
% Plot edge nodes
scatter([edges.position(:,1)], [edges.position(:,2)], 'filled', 'MarkerFaceColor', 'g');
% Plot cloud
scatter(cloud.position(1), cloud.position(2), 'filled', 'MarkerFaceColor', 'r');
% Plot clusters
for i = 1:numClusters
scatter(clusters(i).center(1), clusters(i).center(2), 'x', 'MarkerEdgeColor', 'k', 'LineWidth', 2);
end
% Offload tasks from vehicles to either edge nodes or cloud
for i = 1:numVehicles
% Determine if edge offloading is possible
edgeOffload = false;
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
edgeOffload = true;
break;
end
end
% Offload task to edge node if possible, otherwise to cloud
if edgeOffload
for j = 1:numEdges
if norm(vehicles(i).position - edges(j).position) <= distanceThreshold && ~isempty(edges(j).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
edges(j).tasks = [edges(j).tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to edge ', num2str(j)]);
break;
end
end
else
if ~isempty(cloud.tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
cloud.tasks = [cloud.tasks; task];
disp(['Task from vehicle ', num2str(i), ' offloaded to cloud']);
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - communicationDelayEdge);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
% Record processing time
edgeProcessingTime = [edgeProcessingTime, t - completedTasks];
end
end
% Process tasks at the cloud
if ~isempty(cloud.tasks)
completedTasks = cloud.tasks(cloud.tasks <= t - communicationDelayCloud);
cloud.tasks = setdiff(cloud.tasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
% Record processing time
cloudProcessingTime = [cloudProcessingTime, t - completedTasks];
end
% Process tasks on vehicles (for simplicity, assume immediate processing)
for i = 1:numVehicles
if ~isempty(vehicles(i).tasks)
completedTasks = vehicles(i).tasks(vehicles(i).tasks <= t);
vehicles(i).tasks = setdiff(vehicles(i).tasks, completedTasks);
disp(['Vehicle ', num2str(i), ' processed ', num2str(length(completedTasks)), ' tasks']);
% Record processing time
vehicleProcessingTime = [vehicleProcessingTime, zeros(1, length(completedTasks))]; % Assuming immediate processing
end
end
% Move vehicles (random movement for visualization)
for i = 1:numVehicles
vehicles(i).position = vehicles(i).position + randn(1, 2)*5; % Random movement
end
% Respond to neighbor requests (for visualization, no actual communication)
for i = 1:numVehicles
% Find neighbors of vehicle i
neighbors = [];
for j = 1:numVehicles
if i ~= j && norm(vehicles(i).position - vehicles(j).position) <= distanceThreshold
neighbors = [neighbors, j];
end
end
% Display neighbors
disp(['Vehicle ', num2str(i), ' has neighbors: ', num2str(neighbors)]);
end
hold off;
axis([0 1000 0 1000]); % Set axis limits
title(['Time Step: ', num2str(t)]);
xlabel('X Position');
ylabel('Y Position');
grid on;
pause(0.5); % Pause to visualize each time step
end
% Plot processing time for each processing entity
figure;
histogram(vehicleProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'b', 'EdgeColor', 'w');
hold on;
histogram(edgeProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'g', 'EdgeColor', 'w');
histogram(cloudProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'r', 'EdgeColor', 'w');
hold off;
title('Processing Time Distribution');
xlabel('Processing Time (time steps)');
ylabel('Probability');
legend('Vehicle', 'Edge', 'Cloud');
  5 Comments
Catherine
Catherine on 22 Mar 2024
i got the following error after changing from histogram to hist
Error using hist (line 45)
Input arguments must be numeric.
Error in (line 187)
hist(vehicleProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'b',
'EdgeColor', 'w');
Stephen23
Stephen23 on 23 Mar 2024
Edited: Stephen23 on 23 Mar 2024
"i got the following error after changing from histogram to hist"
Which is why I already told you that HIST supports no name-value input arguments. If you then call HIST with many name-value input arguments (as your quoted error message clearly shows), what do you expect to happen?
You should read the documentation of every function that you use. And follow it.

Sign in to comment.

Accepted Answer

Voss
Voss on 22 Mar 2024
Regarding the error:
Field reference for multiple structure elements that is followed by more reference blocks is an error.
Error in (line 71)
scatter([vehicles.position(:,1)], [vehicles.position(:,2)], 'filled', 'MarkerFaceColor', 'b');
vehicles is a 50x1 structure array; the position field of each element of vehicles is a 1x2 numeric array.
You need to concatenate all the vehicles positions together and then take the first or second column, so this:
% Plot vehicles
pos = vertcat(vehicles.position); % vertical concatentation to get a 50x2 array
scatter(pos(:,1), pos(:,2), 'filled', 'MarkerFaceColor', 'b');
instead of this:
% Plot vehicles
scatter([vehicles.position(:,1)], [vehicles.position(:,2)], 'filled', 'MarkerFaceColor', 'b');
Similiarly for edges. You can do the same for clusters, rather than plotting them in a loop.
But for cloud there is no field called "position", so you'll get an error about that here:
% Plot cloud
scatter(cloud.position(1), cloud.position(2), 'filled', 'MarkerFaceColor', 'r');
I'm not sure what you want to plot from cloud.
  2 Comments
Catherine
Catherine on 22 Mar 2024
Edited: Catherine on 22 Mar 2024
After adjusting i still get this error
Undefined function 'histogram' for input arguments of type 'double'.
Error in (line 187)
histogram(vehicleProcessingTime, 'BinWidth', 1, 'Normalization', 'probability', 'FaceColor', 'b',
'EdgeColor', 'w');
Voss
Voss on 22 Mar 2024
As pointed out elsewhere, you can try to use hist instead of histogram, but the syntax is different.
However, I notice that after I run your code, I get vehicleProcessingTime is all zeros, and edgeProcessingTime and cloudProcessingTime are both empty. Is that expected? If not, check on how those three things are calculated.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!