البرمجة

رسم البيانات من ملفات نصية في MATLAB

To read the text file in MATLAB and plot the data, you can use the textscan function to read the file and then extract the relevant columns for plotting. Assuming that each line in the text file contains data separated by spaces, you can use the following code:

matlab
% Open the file for reading fid = fopen('filename.txt', 'r'); % Read the data using textscan data = textscan(fid, '%s %f %f %f %f %s %f %s', 'Delimiter', ' '); % Close the file fclose(fid); % Extract the columns for plotting x = data{2}; y = data{3}; z = data{4}; % Plot the data scatter3(x, y, z); xlabel('X'); ylabel('Y'); zlabel('Z'); title('Scatter Plot'); % Repeat this code within a loop to process multiple files

Replace 'filename.txt' with the actual path to your text file. This code reads the file line by line, extracting the numeric values for plotting. You can put this code inside a loop to process multiple files.

If you have multiple files with similar content, you can use a loop to read and plot them. Here’s an example of how you can do this:

matlab
filePaths = {'file1.txt', 'file2.txt', 'file3.txt'}; for i = 1:numel(filePaths) fid = fopen(filePaths{i}, 'r'); data = textscan(fid, '%s %f %f %f %f %s %f %s', 'Delimiter', ' '); fclose(fid); x = data{2}; y = data{3}; z = data{4}; scatter3(x, y, z); xlabel('X'); ylabel('Y'); zlabel('Z'); title(['Scatter Plot - File ' num2str(i)]); end

This code assumes that file1.txt, file2.txt, etc., are the names of your text files. Adjust the filePaths array to contain the actual file paths you want to process. The loop will read each file, extract the data, and plot it.

المزيد من المعلومات

لقراءة المزيد من الملفات النصية بنفس الهيكل ورسمها في MATLAB، يمكنك استخدام دالة dir للحصول على قائمة بأسماء الملفات في الدليل الحالي ثم استخدامها في حلقة لقراءة ورسم كل ملف. إليك كيفية القيام بذلك:

matlab
% تحديد الدليل الذي تحتوي على الملفات dirPath = 'المسار_الكامل_للدليل'; % الحصول على قائمة بأسماء الملفات في الدليل files = dir(fullfile(dirPath, '*.txt')); % حلقة لقراءة ورسم كل ملف for i = 1:numel(files) % فحص ما إذا كان الملف هو فعلاً ملف نصي if files(i).isdir continue; end % قراءة الملف fid = fopen(fullfile(dirPath, files(i).name), 'r'); data = textscan(fid, '%s %f %f %f %f %s %f %s', 'Delimiter', ' '); fclose(fid); x = data{2}; y = data{3}; z = data{4}; % رسم البيانات scatter3(x, y, z); xlabel('X'); ylabel('Y'); zlabel('Z'); title(['Scatter Plot - ' files(i).name]); end

قم بتعديل dirPath ليحتوي على المسار الكامل للدليل الذي تريد قراءة الملفات منه. هذا الكود يستخدم dir للحصول على قائمة بأسماء الملفات ذات الامتداد .txt في الدليل، ثم يستخدم حلقة for لقراءة كل ملف ورسمه.

يرجى ملاحظة أنه يجب استبدال 'المسار_الكامل_للدليل' بالمسار الفعلي للدليل الذي تحتوي فيه الملفات النصية التي تريد معالجتها.

مقالات ذات صلة

زر الذهاب إلى الأعلى
إغلاق

أنت تستخدم إضافة Adblock

يرجى تعطيل مانع الإعلانات حيث أن موقعنا غير مزعج ولا بأس من عرض الأعلانات لك فهي تعتبر كمصدر دخل لنا و دعم مقدم منك لنا لنستمر في تقديم المحتوى المناسب و المفيد لك فلا تبخل بدعمنا عزيزي الزائر