% MATLAB Script to Visualize Movement from Excel Data % 1. Specify the Excel file name…
- User
% MATLAB Script to Visualize Movement from Excel Data
% 1. Specify the Excel file name and sheet (if necessary) excelFileName = 'first_draft.xlsx'; % Replace with your Excel file name sheetName = 'Main Data'; % Replace with your sheet name if different
% 2. Read the data from Excel % Using readtable for flexibility with column headers dataTable = readtable(excelFileName, 'Sheet', sheetName);
% Display the first few rows to verify disp('First few rows of the data:'); disp(head(dataTable));
% 3. Extract relevant columns % Modify the column names based on your Excel file's headers % Example assumes columns named 'Time', 'X', 'Y', 'Z' time = dataTable.Time; x = dataTable.X; y = dataTable.Y; z = dataTable.Z;
% Check if Z data exists (for 3D movement) hasZ = any(~isnan(z));
% 4. Plot the movement figure; if hasZ % 3D Plot plot3(x, y, z, '-o', 'LineWidth', 2, 'MarkerSize', 4); grid on; xlabel('X Position'); ylabel('Y Position'); zlabel('Z Position'); title('3D Movement Trajectory'); view(3); % Set default 3D view else % 2D Plot plot(x, y, '-o', 'LineWidth', 2, 'MarkerSize', 4); grid on; xlabel('X Position'); ylabel('Y Position'); title('2D Movement Trajectory'); end
% 5. (Optional) Animate the Movement % Uncomment the following section to create an animation
%{ figure; if hasZ h = plot3(x(1), y(1), z(1), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); grid on; xlabel('X Position'); ylabel('Y Position'); zlabel('Z Position'); title('3D Movement Animation'); hold on; for i = 2:length(x) set(h, 'XData', x(i), 'YData', y(i), 'ZData', z(i)); drawnow; pause(0.05); % Adjust pause for speed end else h = plot(x(1), y(1), 'ro', 'MarkerSize', 8, 'MarkerFaceColor', 'r'); grid on; xlabel('X Position'); ylabel('Y Position'); title('2D Movement Animation'); hold on; for i = 2:length(x) set(h, 'XData', x(i), 'YData', y(i)); drawnow; pause(0.05); % Adjust pause for speed end end %}
% 6. Save the Plot (Optional) % Uncomment to save the figure % saveas(gcf, 'movement_trajectory.png');
Error using . (line 229) Unrecognized table variable name 'Time'.
Error in CLAWCRUSH (line 18) time = dataTable.Time; ^^^^^^^^^^^^^^
- Assistant
The error you're encountering suggests that the Excel file does not contain a column named 'Time', or the table variable name is different from what you're trying to access. To resolve this, you should first ensure that your Excel file does indeed contain headers that match the variable names you're using in your MATLAB script. Here's a step-by-step approach to fixing the issue:
- User
how to do rotating square in matlab?
- Assistant
Creating a rotating square in MATLAB can be achieved by using a combination of basic plotting functions and a loop to update the square's position over time. Here's a simple example of how you can create a rotating square animation:
matlab - User
now i have a coordinate data in excel, rearrange the script for em
- Assistant
- User
I have 8 point of coordinate and i want it to move together, rearrange the scripts for em
- Assistant