Why these two path strings are different? (manual string vs. pwd + fullfile)

조회 수: 9 (최근 30일)
I am trying to load my file using file path defined in string.
However, although types and values of following variable looks the same, strcmp says they are different:
% WORKSPACE SET TO C:\Users\username\certain_path
pwd = "C:\Users\username\certain_path";
s1 = "C:\Users\username\certain_path\data.mat";
s2 = fullfile(pwd,"data.mat");
strcmp(s1,s2)
ans = logical
0
Computer never lies. Therefore it seems that I am missing something.
What would be the problem?
  댓글 수: 1
Dongho Kim
Dongho Kim 2023년 11월 29일
I missed the point that code runner of this site is very likely to be run on Linux systems. Therefore, result on code runner above is valid.
The probloem is that I experienced this issue on my Windows PC. But, since I did not saved any logs for images for it, I can not prove it happened. As I restarted MATLAB, there was no such problem.
One thing I can suspect is that I only used MATLAB as a runner and did all my works by VSCode(With extensions). But, in normal circumstances, this can not be the cause of this problem.
Although even I found myself making a silly question, thank you all for detailed answers.

댓글을 달려면 로그인하십시오.

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
편집: Dyuman Joshi 2023년 11월 29일
The file separator character for the OS you have must be backslash, as you get the result of the comparison to be 0.
Let's take a look at the output for the code ran here -
pwd = "C:\Users\username\certain_path";
s1 = "C:\Users\username\certain_path\data.mat"
s1 = "C:\Users\username\certain_path\data.mat"
s2 = fullfile(pwd,"data.mat")
s2 = "C:\Users\username\certain_path/data.mat"
As this code is running on a Unix server, where the file separator character is a backslash, the strings are not equal, as seen above.
If you would check the same on Windows, where the file separator character is a forwardslash, the outcome of strcmp() would be 1.
Reference - fullfile
ver
----------------------------------------------------------------------------------------------------- MATLAB Version: 23.2.0.2436196 (R2023b) Update 4 MATLAB License Number: 0 Operating System: Linux 5.4.233-0504233-generic #202302250651 SMP Sat Feb 25 12:26:27 UTC 2023 x86_64 Java Version: Java 1.8.0_292-b10 with AdoptOpenJDK OpenJDK 64-Bit Server VM mixed mode ----------------------------------------------------------------------------------------------------- MATLAB Version 23.2 (R2023b) Simulink Version 23.2 (R2023b) 5G Toolbox Version 23.2 (R2023b) Aerospace Blockset Version 23.2 (R2023b) Aerospace Toolbox Version 23.2 (R2023b) Antenna Toolbox Version 23.2 (R2023b) Audio Toolbox Version 23.2 (R2023b) Automated Driving Toolbox Version 23.2 (R2023b) Bioinformatics Toolbox Version 23.2 (R2023b) Bluetooth Toolbox Version 23.2 (R2023b) Communications Toolbox Version 23.2 (R2023b) Computer Vision Toolbox Version 23.2 (R2023b) Control System Toolbox Version 23.2 (R2023b) Curve Fitting Toolbox Version 23.2 (R2023b) DO Qualification Kit Version 23.2 (R2023b) DSP System Toolbox Version 23.2 (R2023b) Database Toolbox Version 23.2 (R2023b) Datafeed Toolbox Version 23.2 (R2023b) Deep Learning Toolbox Version 23.2 (R2023b) Econometrics Toolbox Version 23.2 (R2023b) Embedded Coder Version 23.2 (R2023b) Filter Design HDL Coder Version 23.2 (R2023b) Financial Instruments Toolbox Version 23.2 (R2023b) Financial Toolbox Version 23.2 (R2023b) Fixed-Point Designer Version 23.2 (R2023b) Fuzzy Logic Toolbox Version 23.2 (R2023b) Global Optimization Toolbox Version 23.2 (R2023b) HDL Coder Version 23.2 (R2023b) HDL Verifier Version 23.2 (R2023b) IEC Certification Kit Version 23.2 (R2023b) Image Acquisition Toolbox Version 23.2 (R2023b) Image Processing Toolbox Version 23.2 (R2023b) Industrial Communication Toolbox Version 23.2 (R2023b) Instrument Control Toolbox Version 23.2 (R2023b) LTE Toolbox Version 23.2 (R2023b) MATLAB Compiler Version 23.2 (R2023b) MATLAB Compiler SDK Version 23.2 (R2023b) MATLAB Report Generator Version 23.2 (R2023b) Mapping Toolbox Version 23.2 (R2023b) Mixed-Signal Blockset Version 23.2 (R2023b) Model Predictive Control Toolbox Version 23.2 (R2023b) Navigation Toolbox Version 23.2 (R2023b) Optimization Toolbox Version 23.2 (R2023b) Parallel Computing Toolbox Version 23.2 (R2023b) Partial Differential Equation Toolbox Version 23.2 (R2023b) Phased Array System Toolbox Version 23.2 (R2023b) Powertrain Blockset Version 23.2 (R2023b) Predictive Maintenance Toolbox Version 23.2 (R2023b) RF Blockset Version 23.2 (R2023b) RF Toolbox Version 23.2 (R2023b) Requirements Toolbox Version 23.2 (R2023b) Risk Management Toolbox Version 23.2 (R2023b) Robotics System Toolbox Version 23.2 (R2023b) Robust Control Toolbox Version 23.2 (R2023b) Sensor Fusion and Tracking Toolbox Version 23.2 (R2023b) SerDes Toolbox Version 23.2 (R2023b) Signal Processing Toolbox Version 23.2 (R2023b) SimBiology Version 23.2 (R2023b) SimEvents Version 23.2 (R2023b) Simscape Version 23.2 (R2023b) Simscape Driveline Version 23.2 (R2023b) Simscape Electrical Version 23.2 (R2023b) Simscape Fluids Version 23.2 (R2023b) Simscape Multibody Version 23.2 (R2023b) Simulink 3D Animation Version 23.2 (R2023b) Simulink Check Version 23.2 (R2023b) Simulink Code Inspector Version 23.2 (R2023b) Simulink Coder Version 23.2 (R2023b) Simulink Control Design Version 23.2 (R2023b) Simulink Coverage Version 23.2 (R2023b) Simulink Design Optimization Version 23.2 (R2023b) Simulink Design Verifier Version 23.2 (R2023b) Simulink Desktop Real-Time Version 23.2 (R2023b) Simulink PLC Coder Version 23.2 (R2023b) Simulink Real-Time Version 23.2 (R2023b) Simulink Report Generator Version 23.2 (R2023b) Simulink Test Version 23.2 (R2023b) Stateflow Version 23.2 (R2023b) Statistics and Machine Learning Toolbox Version 23.2 (R2023b) Symbolic Math Toolbox Version 23.2 (R2023b) System Identification Toolbox Version 23.2 (R2023b) Text Analytics Toolbox Version 23.2 (R2023b) Vehicle Dynamics Blockset Version 23.2 (R2023b) Vehicle Network Toolbox Version 23.2 (R2023b) Vision HDL Toolbox Version 23.2 (R2023b) WLAN Toolbox Version 23.2 (R2023b) Wavelet Toolbox Version 23.2 (R2023b) Wireless HDL Toolbox Version 23.2 (R2023b)
  댓글 수: 5
Dongho Kim
Dongho Kim 2023년 11월 30일
"You got the output as 0 when you ran this snippet of code on Windows?"
Yes this is very embarassing but I have experienced it. No remote, no python bridge or any other use cases other than default MATLAB desktop app.
The image you have provided is indeed an expected behavior.
Dyuman Joshi
Dyuman Joshi 2023년 11월 30일
Nothing embarrassing in that. Just a weird/unexpected behaviour.
"One thing I can suspect is that I only used MATLAB as a runner and did all my works by VSCode(With extensions)."
Maybe you can try this again and see what is the output.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Walter Roberson
Walter Roberson 2023년 11월 29일
You will find that on MacOS and Linux (including here on MATLAB Answers) that fullfile uses / separators
  댓글 수: 1
Dongho Kim
Dongho Kim 2023년 11월 29일
Yes, that's correct. I missed the point that code runner on MATLAB Answers is based on Linux as mentioned on comment above. This is a very silly mistake.

댓글을 달려면 로그인하십시오.


John D'Errico
John D'Errico 2023년 11월 29일
LOOK AT THE STRINGS. They are just strings after all. What is different between them?
pwd = "C:\Users\username\certain_path";
s1 = "C:\Users\username\certain_path\data.mat"
s1 = "C:\Users\username\certain_path\data.mat"
s2 = fullfile(pwd,"data.mat")
s2 = "C:\Users\username\certain_path/data.mat"
s1 == s2
ans = logical
0
Look at the difference. On your computer system, the path separator is apparently not \, but / (the forward slash). fulfile knows that.
  댓글 수: 1
Dongho Kim
Dongho Kim 2023년 11월 29일
Since I am very familiar with unix systems, I do know that file separators in Unix systems ans Windows are different.
I tried this code on my Windows system and this problem occurred. It is very unfortunate that I could not take a screenshot but as I restarted matlab, same code showed me 'logical 1'.
I guess something was wrong due to unkown reason, but sadly, I do not have a chance to prove it for now.
I do know this question looks like a silly question. But still, thank you for your answer.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by